How does the Java IO wrapper stream close?

Source: Internet
Author: User

Http://www.cnblogs.com/qqzy168/p/3670915.html

——————————————————————————————————————————————————————

Problem:

(1) The IO stream of Java uses the adornment mode, when closing the outermost stream, it automatically calls the close () side of the wrapped stream?

(2) If the flow is closed sequentially, is it from the inner laminar to the outer stream closed or from the outer to the memory closed?

Question (1) Explanation:

The following example code:  New FileInputStream (".") );     New Bufferedinputstream (IS);    Bis.close (); From the design pattern: Java.io.BufferedInputStream is the Java.io.InputStream decoration class. Bufferedinputstream decorate a inputstream so that it has a buffering function, is to turn off just call the close () method of the object that is finally adorned, because it will eventually invoke the close () method of the true data source object. The InputStream is closed in the Bufferedinputstream Close method, and the following is the source code included with the JDK: Java.io.BufferedInputStream api:closevoid Close ()throws IOException closes this input stream and frees all system resources associated with the stream. 

Therefore, you can only call the Close method of the outer flow to close the inner laminar flow of its adornment, to verify the example:

Throws Exception {        new FileOutputStream ("D:\\a.txt"new OutputStreamWriter (FOS, "UTF-8"   New BufferedWriter (OSW); Bw.write ("Java IO close Test"); Bw.close ();}

Verify OK

Question (2) Explanation: If you do not want to use the (1) way to close the stream, you can close the stream one by one (maybe you are more accustomed to it)

 The following example: 
public static void main (string[] args) throws Exception {FileOutputStream fos = new Fileoutputstrea M ("D:\\a.txt" ); OutputStreamWriter OSW = new outputstreamwriter (FOS, "UTF-8" ); BufferedWriter bw = new BufferedWriter (OSW); Bw.write (" Java IO close test "); // reported exception:

Exception in thread "main" Java.io.IOException:Stream closed
At Sun.nio.cs.StreamEncoder.ensureOpen (streamencoder.java:26)
At Sun.nio.cs.StreamEncoder.write (streamencoder.java:99)
At Java.io.OutputStreamWriter.write (outputstreamwriter.java:190)
At Java.io.BufferedWriter.flushBuffer (bufferedwriter.java:111)
At Java.io.BufferedWriter.close (bufferedwriter.java:246)
At Com.my.test.QQ.main (qq.java:22)


Here's an example:
public static void Main (string[] args) throws Exception {        FileOutputStream fos = new FileOutputStream ("D:\\a.txt"); 
   outputstreamwriter OSW = new OutputStreamWriter (FOS, "UTF-8");        BufferedWriter bw = new BufferedWriter (OSW);        Bw.write ("Java IO close Test");        Close OK Bw.close () from outside to inside order        ;        Osw.close ();        Fos.close ();    }

Verify OK

Generally: first open after close, then open first close

Another scenario: Look at dependencies, if flow a relies on flow B, you should close stream A and then close stream b

For example, process flow a relies on node stream B, you should close process flow A and then close node stream b

Of course, you can simply close the processing stream without shutting down the node stream. When the processing stream is closed, it calls the closing method of the node stream it handles

If the node stream closes and then closes the processing stream, an IO exception is thrown;

How does the Java IO wrapper stream close?

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.