The System.out
variable is referencing an object of type PrintStream
which wraps a BufferedOutputStream
(at least in Oracle JDK 7). When you call one of the printX()
or write()
methods on PrintStream
, it internally flushes the buffer of the underlying BufferedOutputStream
.
That doesn't happen with PrintWriter
. You have to do it yourself. Alternatively, you can create a PrintWriter
with an autoFlush
property set to true
which will flush on each write.
PrintWriter pw = new PrintWriter(System.out, true);
If you read this constructor's javadoc, it states
- autoFlush A boolean; if true, the println, printf, or format methods will flush the output buffer