Is it necessary to close each nested OutputStream and Writer separately?

后端 未结 7 2066
醉酒成梦
醉酒成梦 2020-11-28 02:28

I am writing a piece of code:

OutputStream outputStream = new FileOutputStream(createdFile);
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputS         


        
7条回答
  •  無奈伤痛
    2020-11-28 03:08

    If all of the streams have been instantiated then closing only the outermost is just fine.

    The documentation on Closeable interface states that close method:

    Closes this stream and releases any system resources associated with it.

    The releasing system resources includes closing streams.

    It also states that:

    If the stream is already closed then invoking this method has no effect.

    So if you close them explicitly afterwards, nothing wrong will happen.

提交回复
热议问题