Here from the java doc:
PrintWriter
public PrintWriter(Writer out, boolean autoFlush)
Creates a new PrintWriter. Parameters: out - A character-output stream autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
I would like to know if creating a PrintWriter
with this constructor actually wrap the OutputStream
will be buffered or not.
Here is the source code I found in this website (grepcode) and here I report the class constructor in question:
Creates a new PrintWriter.
Parameters: out A character-output stream autoFlush A boolean; if true, the println, printf, or format methods will flush the output buffer.
public [More ...] PrintWriter(Writer out,boolean autoFlush) { super(out); this.out = out; this.autoFlush = autoFlush; lineSeparator = java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction("line.separator")); }
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters: out An output stream See also: java.io.OutputStreamWriter.OutputStreamWriter.(java.io.OutputStream) public [More ...] PrintWriter(OutputStream out) { this(out, false); }
Note that the super(out)
it's only needed for synchronization purposes.(source code here)
There is no reference of using a BufferedWriter
as there is for all the other constructors of the PrintWriter
class.
In this question I have been told that this constructor does not seem to use a buffer, however when using it it clearly use a buffer (ie without specifying true
in the second parameter it does not flush and flushing is applicable only to buffers.
Moreover, I have seen code written like this:
PrintWriter writer = new PrintWriter(
new BufferedWriter (
new FileWriter("somFile.txt")));
Which could be replace by PrintWriter writer = new PrintWriter(new FileWriter("someFiles.txt",true));
without wrapping the FileWriter
in a buffer.
Bottom line I would like to know if new PrintWriter(writer w, true);
is automatically wrapped in a buffer like all the other constructor are. If so could you please indicate the bit of source code where it happens?