What is the purpose of flush() in Java streams?

Venkat picture Venkat · Feb 26, 2010 · Viewed 233.7k times · Source

In Java, flush() method is used in streams. But I don't understand what are all the purpose of using this method?

fin.flush();

tell me some suggestions.

Answer

codaddict picture codaddict · Feb 26, 2010

From the docs of the flush method:

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The buffering is mainly done to improve the I/O performance. More on this can be read from this article: Tuning Java I/O Performance.