search

Home  >  Q&A  >  body text

java - inputStream关闭了,还有必要关闭InputStreamReader和BufferedReader吗?

PHP中文网PHP中文网2807 days ago1207

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:32:18

    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

    reply
    0
  • 阿神

    阿神2017-04-18 09:32:18

    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 of BufferedReader and 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"

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 09:32:18

    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

    reply
    0
  • Cancelreply