Everyone in the Java IO stream using OutputStream, PrintWriter ... , the Flush () method is often used.
Like caching in network hardware, streams can also be cached in software, directly in Java code. This can be achieved by Bufferedoutputstream or bufferedwriter links to the underlying stream.
Therefore, flush becomes particularly important when you finish writing data.
For example:
The Web server responds with a 300-byte message through the output flow to the client, but at this point the output stream has a 1024-byte buffer. Therefore, the output stream waits for the Web server to continue to respond to the client, and when the Web server's response information fills the buffer in the output stream, the output stream responds to the message to the Web client.
In order to solve this embarrassing situation, the flush () method appeared. The flush () method can force the output stream (or buffered stream) to send data, even if the buffer is not filled at this time, to break the state of the deadlock.
When we send data using the output stream, when the data cannot fill the buffer of the output stream, the data is stored in the buffer of the output stream. If we call the close output stream at this time, the data stored in the buffer of the output stream will be lost.
So, when you close the output stream, you should flush the output stream of the flush, saying, "Forcing all buffered output data to be written out to the underlying output stream".
Uncover the mysteries of flush () in the Java IO stream