File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

FileWriter, makes new file but no text shows

Jim Standish

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?
[ February 24, 2006: Message edited by: Jim Standish ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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]
I agree. Here's the link:
subject: FileWriter, makes new file but no text shows
jQuery in Action, 3rd edition