permaculture playing cards*
The moose likes I/O and Streams and the fly likes FileWriter, makes new file but no text shows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FileWriter, makes new file but no text shows" Watch "FileWriter, makes new file but no text shows" New topic
Author

FileWriter, makes new file but no text shows

Jim Standish
Greenhorn

Joined: Feb 24, 2006
Posts: 5
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
Don't get me started about those stupid light bulbs.
 
subject: FileWriter, makes new file but no text shows
 
Similar Threads
BufferedWriter to byte []
File I/O
Need help writing a text file
write a int to a file
Need help writing to a text file