public PrintWriter(OutputStream out, boolean autoFlush):
out - An output stream autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
public PrintStream(OutputStream out, boolean autoFlush):
out - The output stream to which values and objects will be printed autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written
What was the reason for changing autoflush logic between these classes?
Because they are always considered as identical except for encoding moments and "autoflush" without flushing on print() hardly corresponds to principle of least astonishment, silly bugs occur:
I created a PrintWriter with autoflush on; why isn't it autoflushing?
I think the answer lies in the history of Java. The trio
InputStream,OutputStreamandPrintStreaminjava.iodate back to Java 1.0. That is before serious support for file encodings and character sets were built into the language.To quote the Javadoc:
To summarize, it is a convenience for generating textual output, grafted on top of lower level IO.
In Java 1.1,
Reader,WriterandPrintWriterwere introduced. Those all support character sets. WhileInputStreamandOutputStreamstill had a real uses (raw data processing),PrintStreambecame far less relevant, because printing by nature is about text.The Javadoc for
PrintWriterexplicitly states:Put another way, PrintWriter should only be used through the
print*(...)APIs, because writing newline characters etc should not be the caller's responsibility, the same way dealing with file encodings and character sets are not the caller's responsibility.I would argue that
PrintWritershould have beenjava.io.Printerinstead, and not have extendedWriter. I don't know whether they extended to mimicPrintStream, or because they were stuck on maintaining the pipe design idiom.