• Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintStream print method autoflushing  RSS feed

 
Tuhin Chatterjee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:




The javadoc entry for java.lang.PrintStream states:

"Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written".

However, when I run this code, the buffer is getting flushed even for the print method. In fact, when you turn off autoflush, the print method stops printing. Why is the autoflush affecting the print method? Shouldn't it only affect the println method?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Stream or Writer is free to flush itself any time. The autoFlush only guarantees that it will happen in the listed circumstance, not that it won't happen in any others. It may be an error in the documentation, or it may be that in that implementation it was just easier to make print() flush whenever println() does as well, or it may be something else.

The code for the core API classes in in src.zip in the JDK download, so you can look at it and see exactly what it's doing if you're curious.
 
Tuhin Chatterjee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff. Also, correct me if I'm wrong.

From what little I studied the IO class hierarchy, all Input Streams are unbuffered.

In case of Output Streams, both java.lang.PrintStream and java.lang.PrintWriter are buffered; whereas java.lang.FileOutputStream, java.lang.OutputStreamWriter and java.lang.FileWriter are unbuffered, for which we need the respective buffered versions to wrap around them and make them buffered?

Finally, can there be errors in the Java Docs?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tuhin Chatterjee wrote:From what little I studied the IO class hierarchy, all Input Streams are unbuffered.

How can that be? If a BufferedInputStream is an InputStream (this is the basic idea of a subclass), then doesn't that suggest that at least some InputStreams are buffered?

Tuhin Chatterjee wrote:In case of Output Streams, both java.lang.PrintStream and java.lang.PrintWriter are buffered; whereas java.lang.FileOutputStream, java.lang.OutputStreamWriter and java.lang.FileWriter are unbuffered, for which we need the respective buffered versions to wrap around them and make them buffered?

I don't think any of those are guaranteed to be buffered, according to the docs. An implementation may have an undocumented buffer. But if you want to be sure, add a BufferedOutputStream yourself.

Tuhin Chatterjee wrote:Finally, can there be errors in the Java Docs?

Sure, it's possible. I don't think anything you've described here is an error in the javadocs. But mistakes have been known to happen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!