Venkat Venkat - 4 months ago 24
Java Question

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

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

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.