Home / Java Patterns and Pitfalls     frequal.com

Make PrintStream Faster

PrintStream is useful for outputting line-oriented data. However, the obvious constructor suffers from a potential performance problem: every call to print() is followed by an automatic flush. autoflushing prevents standard write buffering from providing its usual performance benefits, even if the stream given to the PrintStream is a buffered one, like BufferedOutputStream. When PrintStream is used as part of a batch processing application that outputs a large amount of data, this problem can cause output to be much slower than it needs to be.

The solution is simple: use the two-parameter constructor and pass false for the second parameter. That disables the autoflush behavior and lets write buffering speed up your output.

Last modified on 16 May 2001 by AO

Copyright © 2016 Andrew Oliver