Home > Net >  Why does the PrintWriter class (and other writers) require a call to flush after writing?
Why does the PrintWriter class (and other writers) require a call to flush after writing?

Time:05-07

I have noticed that some I/O Classes in Java (and a lot others, like the BufferedWriter and FileWriter), require a call to flush() after writing. (With the exception of AutoFlush, I'll get to that later).

For example, this call to println() will not work. However, if I invoke writer#flush() after, the line will print.

PrintWriter writer = new PrintWriter(System.out);
writer.println("test");

Also, does autoflushing impact performance in any way (especially in larger/consistent writes), or is it just a convenience, and is it recommended to use it?

CodePudding user response:

Why does the PrintWriter class (and other writers) require a call to flush after writing?

To the extent that flushing is wanted1, it will be needed if the "stack" of output classes beneath the print writer is doing some output buffering. If an output stream is buffered, then some event needs to trigger pushing (flushing) the buffered output to the the external file, pipe, socket or whatever. The things that will trigger flushing are:

  • the buffer filling up
  • something calling close() on the stream, or
  • something calling flush() on the stream.

In the case of a PrintWriter, the underlying stream can also be flushed by the classes auto-flushing mechanism.

The reason for buffering output (in general) is efficiency. Performing the low-level output operation that writes data to the (external) file, pipe, whatever involves a system call. There are significant overheads in doing this, so you want to avoid doing lots of "little" writes.

Output buffering is the standard way to solve this problem. Data to be written is collected in the buffer until the buffer fills up. The net result us lots of "little" writes can be aggregated into a "big" write. The performance improvement can be significant.


Also, does autoflushing impact performance in any way (especially in larger/consistent writes), or is it just a convenience, and is it recommended to use it?

It is really a convenience to avoid having to explicitly flush. It doesn't improve performance. Indeed, if you don't need the data to be flushed1, then unnecessary auto-flushing will reduce performance.


1 - You would want the data to be flushed if someone or something wants to see the data you are writing as soon as possible.

CodePudding user response:

They don't require flushing, only if you want to guarantee that output has been displayed so far, which is exactly what flushing is. If you are fine writing to a file and just want to make sure it gets there before the program terminates, then no need to flush.

CodePudding user response:

When data is written to an output stream, the underlying an operating system does not guarantee that the data will make it to the file system immediately. In many operating systems, the data may be cached in memory, with a write occurring only after a temporary cache is filled or after some amount of time has passed.

If the data is cached in memory and the application terminates unexpectedly, the data would be lost, because it was never written to the file system. To address this, all output stream classes provide a flush() method, which requests that all accumulated data be written immediately to disk.

The flush() method helps reduce the amount of data lost if the application terminates unexpectedly. It is not without cost, though. Each time it is used, it may cause a noticeable delay in the application, especially for large files. Unless the data that you are writing is extremely critical, the flush() method should be used only intermittently. For example, it should not necessarily be called after every write.

You also do not need to call the flush() method when you have finished writing data, since the close() method will automatically do this.

Read from the book here -> OCP Oracle Certified Professional Java SE 11

Hope this is clear to you!

  • Related