Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PrintWriter buffered?

I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance.

But I tried something like this,

PrintWriter pw = new PrintWriter(System.out);
pw.println("Statement 1");
pw.println("Statement 2");
//pw.flush();

I observed that when the flush method is commented there is no output, but when I uncomment it, I get the desired output.

This is only possible if the PrintWriter is buffered. If so, then what is the point of wrapping a PrintWriter using a BufferedWriter and then writing it?

Though the javadoc doesn't mention anywhere that the PrintWriter is buffered, but it seems so.

like image 772
Aritra Roy Avatar asked Aug 24 '15 08:08

Aritra Roy


People also ask

Is PrintWriter faster than system out?

PrintWriter class is the implementation of Writer class. By using PrintWriter than using System. out. println is preferred when we have to print a lot of items as PrintWriter is faster than the other to print data to the console.

What is the difference between BufferedWriter and PrintWriter?

The biggest difference is that the print and println methods of PrintWriter take arguments of any type, generally calling the toString() or String. valueOf() methods to get String objects. The BufferedWriter write() method takes a single character, an array of characters, or a String.

What are the features of PrintWriter?

Unlike other writers, PrintWriter converts the primitive data ( int , float , char , etc.) into the text format. It then writes that formatted data to the writer. Also, the PrintWriter class does not throw any input/output exception.

Is PrintWriter an output stream?

Class PrintWriter. Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream . It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.


2 Answers

I checked JDK versions starting with 1.6.0_45 and all of them have this constructor present:

/**
 * Creates a new PrintWriter from an existing OutputStream.  This
 * convenience constructor creates the necessary intermediate
 * OutputStreamWriter, which will convert characters into bytes using the
 * default character encoding.
 *
 * @param  out        An output stream
 * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
 *                    <tt>printf</tt>, or <tt>format</tt> methods will
 *                    flush the output buffer
 *
 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
 */
public PrintWriter(OutputStream out, boolean autoFlush) {
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);

Hence PrintWritter uses buffered output. If you would like to use the code you pointed out, you can create the PrintWriter with the autoflush set to true, that will ensure that using one of the println, printf or format methods would flush the stream. So, your code would look like this in the given context:

PrintWriter pw = new PrintWriter(System.out, true);
pw.println("Statement 1");
pw.println("Statement 2");
like image 155
Olimpiu POP Avatar answered Sep 25 '22 11:09

Olimpiu POP


From the Java 8 source for PrintWriter

/**
 * Creates a new PrintWriter from an existing OutputStream.  This
 * convenience constructor creates the necessary intermediate
 * OutputStreamWriter, which will convert characters into bytes using the
 * default character encoding.
 *
 * @param  out        An output stream
 * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
 *                    <tt>printf</tt>, or <tt>format</tt> methods will
 *                    flush the output buffer
 *
 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
 */
public PrintWriter(OutputStream out, boolean autoFlush) {
    this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);

You can see that PrintWriter uses BufferedWriter and that it has an option autoFlush which would only make sense if it was buffered.

like image 38
Peter Lawrey Avatar answered Sep 22 '22 11:09

Peter Lawrey