The flush () method of the output stream in java

Source: Internet
Author: User

The flush () method of the output stream in java

In java Development, we sometimes perform stream operations, so we may often encounter such a piece of code

 

out.flush();out.close();
Sometimes we only understand this, but do not know the Principles. Let's take a look at the following:

 

The flush () function is used to clear the data streams in the buffer zone. During stream operations, the data is first read into the memory and then written to the file, when you read data, if we call the close () method to close the read/write stream at this time, data may be lost. Why, when the data is read, it does not mean that the data is written completely. Some data may be left in the cache. To understand this problem, let's take an example:

For example, in rural areas, almost every household has a water pumping machine. What is the role of water pumping machine? That is, the water in the water well is drawn into the water tank, at this time, we will use a water pipe to connect the water pumping machine and the water tank (the water pipe is like a buffer zone). When we want to pump all the water in the water tank, we will let the water pumping machine work to pump water, if we find that the water in the water well is just finished, we will turn off the water pumping switch to stop pumping, then some water will be left in the pipeline, pumping is to read data, the water tank is used to write data, and the water pipe acts as the cache area. I don't know if this is concrete.

In this way, if we call the close () method halfway, there will still be data in the output zone, just like water in the water tank, but a part left in the buffer zone, at this time, if we call the flush () method first, we will force the data output, the cache will be cleared, and then close the read/write stream and call close.

 

 public class PrintWriter extends Writer {    protected Writer out;/** Checks to make sure that the stream has not been closed */    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 JDK. As mentioned above, the ensureOpen () method is used to ensure that the stream is not closed. If it is not closed, the out method in the flush method is executed. flush ();

 

Does this list have an understanding of the methods in PrintWriter. The problem arises again. Is the flush () We usually use the same as the flush () here? Think about it first.

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.