This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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: 24189

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: FileWriter, makes new file but no text shows
It's not a secret anymore!