my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes Is PrintWriter Buffered? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Is PrintWriter Buffered?" Watch "Is PrintWriter Buffered?" New topic
Author

Is PrintWriter Buffered?

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
If we want buffered output to a file, should we wrap the printWriter around
a BufferedWriter like this:



The javadoc doesn't mention if PrintWriter is buffered or not. I want to use PrintWriter for writing formatted text string to a file but want it to be buffered.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41586
    
  54
Since you're using a BufferedWriter, it doesn't matter much whether the PrintWriter is buffered (and I think it isn't, because that's what BW is for). The important thing is that the data does get buffered before being written to disk, and that's what happens here.


Ping & DNS - my free Android networking tools app
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Thanks for your advice. Actually, I'm worrying about buffering twice if PrintWriter already buffered.
Ilker Tasci
Greenhorn

Joined: Nov 20, 2011
Posts: 1
Yes the PrintWriter is buffered.

See my little test code below:



I attached my pw to the console output.This code will not print the output to the screen because pw.flush() is not invoked(commented as you see).If you uncomment it,you'll see the output on the screen becuase it "flushes" its buffer and sends the data to the output stream which is the console output here.

Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is PrintWriter Buffered?