• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant save a text file  RSS feed

 
Maysam Sorkhabi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi masters,
I'm trying to get some text from a JTextArea and save it to a text file using JFileChooser. Here is my code:

jfc = new JFileChooser() ;
int retValue = jfc.showSaveDialog(jDesktopPane1) ;
if(retValue == JFileChooser.APPROVE_OPTION)
{
File target = jfc.getSelectedFile() ;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(target)) ;
pw.println(jTextArea1.getText()) ;
}
catch(IOException e)
{
jTextArea1.append("An Exception occured") ;
}
This correctly creats a file but when I open it up there's nothing in it(the file name I choose has a .txt extension so I can see it in notepad)

Any thoughts?!
I'd appreciate your comments
Thanks
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to close the two streams. Sometimes that forces the stream to flush the data. It solved a similar problem for me.

Cheers,
Rachel
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!