Java 关闭阅读器后是否需要关闭 InputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3628899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:54:47  来源:igfitidea点击:

Do I need to close InputStream after I close the Reader

java

提问by Cheok Yan Cheng

I was wondering, whether is there any need for me to close the InputStream, after I close the reader?

我想知道,关闭阅读器后是否需要关闭InputStream?

    try {
        inputStream = new java.io.FileInputStream(file);
        reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(reader)) {
            return null;
        }
        // Do I need to close inputStream as well?
        if (false == close(inputStream)) {
            return null;
        }
    }

采纳答案by Hyman

No, you don't have to.

不,你不必。

Since the decorator approach used for streams in Java can build up new streams or reader by attaching them on others this will be automatically be handled by InputStreamReaderimplementation.

由于在 Java 中用于流的装饰器方法可以通过将它们附加到其他流或读取器来构建新的流或读取器,这将由实现自动处理InputStreamReader

If you look at its source InputStreamReader.javayou see that:

如果你查看它的来源,InputStreamReader.java你会看到:

private final StreamDecoder sd;

public InputStreamReader(InputStream in) {
  ...
  sd = StreamDecoder.forInputStreamReader(in, this, (String)null);
  ...
}

public void close() throws IOException {
  sd.close();
}

So the close operation actually closes the InputStreamunderlying the stream reader.

所以关闭操作实际上关闭了InputStream底层的流读取器。

EDIT: I wanna be sure that StreamDecoderclose works also on input stream, stay tuned.

编辑:我想确保StreamDecoderclose 也适用于输入流,请继续关注。

Checked it, in StreamDecoder.java

检查了一下,在 StreamDecoder.java

void implClose() throws IOException {
  if (ch != null)
    ch.close();
  else
    in.close();
}

which is called when sd's close is called.

当调用 sd 的 close 时调用它。

回答by Jon Freedman

No you don't the reader will close the underlying InputStream

不,你不阅读器会关闭底层的 InputStream

回答by helios

Acordding to source sniffing the reader closes its underlying inputstream. According to javadoc it seams that InputStreamReader "closes the stream" when reader.close() is invoked.

根据源嗅探阅读器关闭其底层输入流。根据 javadoc,当调用 reader.close() 时,InputStreamReader 会“关闭流”。

I'm not sure if ANY Reader must close its sources when you do reader.close(). I think that this is important so your code can use a reader no matter what concrete type it is.

当您执行 reader.close() 时,我不确定是否有任何 Reader 必须关闭其源。我认为这很重要,因此您的代码可以使用阅读器,无论它是什么具体类型。

Anyway it makes sense that it's enforced.

无论如何,它被强制执行是有道理的。

回答by amorfis

You don't have to close stream, if you close()the reader.

如果您close()是读者,则不必关闭流。

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

关闭流并释放与其关联的任何系统资源。一旦流关闭,进一步的 read()、ready()、mark()、reset() 或 skip() 调用将抛出 IOException。关闭先前关闭的流没有任何效果。

回答by Tom Hawtin - tackline

Technically, closing the Readerwill close the InputStream. However, if there was a failure between opening the InputStreamand creating the Reader, you should still close the InputStream. If you close the InputStream[the resource] there shouldn't be a good reason to close the Reader[the decorator]. There are also popular bugs where closing a decorator can throw an exception before closing the decorated. So:

从技术上讲,关闭Reader将关闭InputStream. 但是,如果在打开InputStream和创建之间出现故障Reader,您仍然应该关闭InputStream. 如果您关闭InputStream[资源],就没有理由关闭Reader[装饰器]。还有一些流行的错误,在关闭装饰器之前关闭装饰器可能会引发异常。所以:

Resource resource = acquire();
try {
    Decorator decorated = decorate(resource);
    use(decorated);
} finally {
    resource.release();
}

There are some complications to watch out for. Some decorators may actually contain native resources due to their implementation. Output decorators will generally need to be flushed, but only in the happy case (so in the trynot the finallyblock).

有一些并发症需要注意。由于它们的实现,一些装饰器实际上可能包含本地资源。输出装饰器通常需要刷新,但仅在满意的情况下(因此在trynotfinally块中)。