我是否需要在Java中关闭InputStream? [英] Do I need to close an InputStream in Java?

查看:1363
本文介绍了我是否需要在Java中关闭InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

InputStream confFile=classLoader.getResourceAsStream("myconffile.properties");

在文档中:


InputStream的close方法什么都不做。

The close method of InputStream does nothing.

这是否意味着我不需要关闭InputStream?

Does it mean that I don't need close InputStream?

推荐答案

您需要关闭输入Stream,因为您提到的方法返回的流实际上是 FileInputStream 或其他一些 InputStream 的子类,它包含文件的句柄。如果您不关闭此流,则会导致资源泄漏。

You do need to close the input Stream, because the stream returned by the method you mention is actually FileInputStream or some other subclass of InputStream that holds a handle for a file. If you do not close this stream you have resource leakage.

这篇关于我是否需要在Java中关闭InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆