InputStreamReader
的 JavaDoc 没有说明如何关闭底层的 InputStream
:
https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#close--
从类中复制的描述:Reader
关闭流并释放与其关联的所有系统资源。一旦流被关闭,进一步的 read()、ready()、mark()、reset() 或 skip() 调用将抛出 IOException。关闭先前关闭的流没有任何效果。
关闭 InputStreamReader 是否也会关闭底层的 InputStream?
更新:
InputStreamReader istream = new InputStreamReader(conn.getInputStream(), "UTF-8")
istream.close();
我需要关闭 conn.getInputStream()
吗?
InputStreamReader
的实现将close
调用直接传递给本地类StreamDecoder
。
InputStreamReader
会关闭StreamDecoder
,而StreamDecoder
会关闭与其链接的InputStream
。请参考源代码。 - AxelHInputStreamReader.close()
会调用InputStream
的close()
方法;而这个方法所做的事情对于InputStreamReader
来说相当不相关。 - Andy Turner