• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter won't write to a text file.....  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have declared the following:



I then tried to write to an output file with:



The System.out.println sends everything to the console window perfectly, but my output file remains blank and it doesn't seem to want to write to the file. Any help?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Blais:
I have declared the following:
The System.out.println sends everything to the console window perfectly, but my output file remains blank and it doesn't seem to want to write to the file. Any help?


Try...

check API for details...
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I did a bit of researching and ended up using out.flush(). Same effect I guess.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.flush() will take all the buffered data in the memory to the disk. you still can write something more.

out.close() will finalize the writing and close the connection stream to the file, you can't write anything anymore.
 
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try without BufferedWriter
PrintWriter out = new PrintWriter(new FileWriter(outFileName));
out.flush();
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use out.close() without invoking out.flush(), it will flush buffered data automatically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!