FileInputStream fis = new FileInputStream(gzipFile);
GZIPInputStream gis = new GZIPInputStream(fis);
gis.close();
fis.close();
fis.close() 是否必要?虽然我运行这段代码似乎没有出现任何错误。
FileInputStream fis = new FileInputStream(gzipFile);
GZIPInputStream gis = new GZIPInputStream(fis);
gis.close();
fis.close();
fis.close() 是否必要?虽然我运行这段代码似乎没有出现任何错误。
GZIPInputStream.close()
的实现。/**
* Closes this input stream and releases any system resources associated
* with the stream.
* @exception IOException if an I/O error has occurred
*/
public void close() throws IOException {
if (!closed) {
super.close();
eos = true;
closed = true;
}
}
GZIPInputStream
的构造函数,它应该是这样的:/**
* Creates a new input stream with the specified buffer size.
* @param in the input stream
* @param size the input buffer size
* @exception IOException if an I/O error has occurred
* @exception IllegalArgumentException if size is <= 0
*/
public GZIPInputStream(InputStream in, int size) throws IOException {
super(in, new Inflater(true), size);
usesDefaultInflater = true;
readHeader(in);
}
请注意变量in
。 注意它是如何传递给超类的,这里是InflaterInputStream
。
现在,如果我们看一下InflaterInputStream.close()
方法的实现,我们会发现这个:
/**
* Closes this input stream and releases any system resources associated
* with the stream.
* @exception IOException if an I/O error has occurred
*/
public void close() throws IOException {
if (!closed) {
if (usesDefaultInflater)
inf.end();
in.close();
closed = true;
}
}
很明显,正在调用in.close()
。因此,在调用GZIPInputStream.close()
时,包装的(装饰的)FileInputStream
也会关闭。这使得调用fis.close()
成为多余的。
GZIPInputStream
覆盖了其父类中的 close
方法,并没有对其进行文档说明(文档质量较差)。但是,很有可能(即使不看代码)它最终会调用 super.close()
(实际上从 adarshr 的回答中我们可以看到它确实这样做了,尽管您不应该假设实现不会改变)。如果是这样的话,那么我们就要查看父类(InflaterInputStream
)的文档。不幸的是,它也完全相同地覆盖了方法而没有文档说明。但是请假设它也在某个时候调用了 super.close()
。查看其父类(FilterInputStream
)的文档,它明确说明在 in
成员上执行了 close
操作,该成员是通过构造函数设置的。(另一个假设是,GZIPInputStream
和 InflaterInputStream
将构造函数参数传递给它们的超类,但这非常有可能。)
super
不是FileInputStream
,而是GZIPInputStream
超类的close
方法。这与构造函数的输入参数(直接)无关。 - T.J. Crowdersuper.close()
的实现,您会看到一行代码in.close();
,其中in
是传递给构造函数的输入流(在OP的情况下是fis
)。 - Thomassuper.close()
并不意味着关闭通过参数传递的流(事实上,您可以确定super.close()
不会这样做,因为它没有访问该属性)。 - SJuan76super
不是FileInputStream
这一事实。但我会让答案更清晰。包装流是间接关闭的。 - adarshrGZIPInputStream
和InflaterInputStream
中的文档不良引起的。叹气 - T.J. Crowder