java - inputStream关闭了,还有必要关闭InputStreamReader和BufferedReader吗?
PHP中文网
PHP中文网 2017-04-18 09:30:18
0
3
1139
PHP中文网
PHP中文网

认证0级讲师

reply all (3)
Ty80

Just call the outermost close() directly


update:

http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#close--

Closes the stream and releases any system resources associated with it

http://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#close--

Closes the stream and releases any system resources associated with it

It’s a chain. You close the outermost one, and the outermost one closes the inner ones. The inner ones close the inner ones


Of course, if you insist on saying that the shutdown may fail, you must finally do it. I can’t help it

    阿神

    If the question is:BufferedReader is closed, is it necessary to close InputStreamReader and InputStream?, then this problem is normal...

    The stream and reader in Java's io package both use the decorator pattern. You only need to call the close method of the outermost decorator, and it will also close the stream or reader it decorates.

    If that doesn’t work, open the source code ofBufferedReaderand take a look at the close method. You will find that it closes the reader it decorates in this method.

    No matter how hard it is, I recommend the following article, which may be able to solve your doubts "Java IO: Streams, and the application of the decorator pattern on it"

      刘奇

      You have to close them all, one by one in order, preferably in finally

      finally{ try{ br.close(); isr.close(); is.close(); }catch(Exception e){ ...... } }

      The first level created is the last level, the last level created is the first level

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!