• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is PrintWriter Buffered?

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your advice. Actually, I'm worrying about buffering twice if PrintWriter already buffered.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic