Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileWriter, makes new file but no text shows  RSS feed

 
Jim Standish
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a text file and write to it, but I can't seem to get the text to post even though the file does get created.

I have tried: FileWriter out = new FileWriter("fileOut", true );

as well as:
BufferedWriter out = new BufferedWriter(new FileWriter("fileOut", true));

and then: out.write("string stuff" + numStuff);

Any suggestions?
-JS
[ February 24, 2006: Message edited by: Jim Standish ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Welcome to JavaRanch!

The short answer: call close() on the BufferedWriter before exiting your program.

Longer answer: the BufferedWriter will hold the data in an internal buffer (hence the name!) until it gets a certain amount; then the buffer is full, and it's flushed all at once out to the file. You're writing a tiny bit, which is still in the buffer when the program exits. The file itself is closed automatically, but not the BufferedWriter, and so its buffered data is simply lost.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!