The shutdown of each stream when multiple streams are nested (such as Bufferedwrite).

Source: Internet
Author: User

When nesting with multiple streams is used, it is always customary to turn off each stream from the inside out.

Today just know, when superfluous, only need to close the outermost layer of the mouth = flow on the line.

Like what:

FileWriter fileWritter1 = new FileWriter (OUTFILE1);
BufferedWriter bw1 = new BufferedWriter (fileWritter1);


Close only need to close the filewritter on it:

Filewritter1.close ();


The reason is in BufferedWriter source code, this is the BufferedWriter close () method source code:



This out object is however a writer:


This writer is precisely the writer that is passed in the BufferedWriter construction parameters, that is, the filewriter in this example. So when you close BufferedWriter,

In fact, nothing has been done, just simply call the FileWriter close () method. And the BufferedWriter Close () method is also a

Synchronized method. It can also cause efficiency problems in multi-threaded situations. So when you nest a multi-layer stream, you just need to close the outermost stream.


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.