Implemented through Bufferedoutputstream or bufferedwriter links to the underlying stream. Therefore, in writing
When the data is finished, flush becomes particularly important.
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".
Two. Read the flush () source code:
The following is an example of the Bufferedoutputstream class:
extends Filteroutputstream { publicsynchronizedvoid flush () throws ioexception { flushbuffer (); Out.flush (); } Privatevoid Flushbuffer () throws ioexception { if(Count > 0) { 0, count); = 0;}} }
See here everyone understand, actually flush () is also through Out.write () to write data to the underlying output stream.
Flush () in the Java IO stream