About the output stream Flush () method in Java

Source: Internet
Author: User

In Java development, sometimes we do the flow of operations, so you may often encounter such a piece of code

Out.flush (); Out.close ();
Sometimes we just understand this, but do not know the principle of the things, the following to understand:

Flush () This function is empty meaning, used to clear the buffer of the data flow, the operation of the stream, the data is read into memory, and then write to the file, then when you read the data, if we call the close () method to close the read and write stream, this can cause data loss, why, In order to understand this problem, we give an example, because, when the read-in data is completed, the write data does not complete, and some of the data may be left in the buffer.

For example, in the countryside, almost every family has a pump, what is the role of the pump, is to pump water in the wells to the tank, then we will use a water pipe connection pump and tank (water pipe is like a buffer), when we want to pump water in the well into the tank, we let the pump work pumping, If we find the water in the well is just finished, we will switch off the pump to stop pumping, then, the pipeline will leave a part of the water, pumping is reading data, water tank is writing data, water pipe as a buffer role, do not know this is not a figurative

So if we call the close () method halfway, the output area still has data, like water in the tank, just a part of the buffer, if we first call the flush () method, it will force the data output, the buffer is emptied, and then closed read and write stream call Close () is done.

public class PrintWriter extends writer {    protected writer out;/** Checks to make sure that the stream have not been CL osed */    private void Ensureopen () throws IOException {        if (out = null)            throw new IOException ("Stream closed") ;    }    /**     * Flushes the stream.     * @see #checkError ()     */public    void Flush () {        try {            synchronized (lock) {                ensureopen ();                Out.flush ();            }        }        catch (IOException x) {            trouble = true;        }}    }
This is the flush method provided by the PrintWriter class in the JDK, as noted above, and the Ensureopen () method is used to ensure that the stream is not closed and executes Out.flush () in the Flush method if it is not closed;

Does this enumeration have some understanding of the methods in PrintWriter? So the question comes again, is the flush () we normally use the same as flush () here? Let's think about this.

About the output stream Flush () method in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.