关闭嵌套 Reader
从文本文件读取时,通常会创建一个 FileReader,然后将其嵌套在 BufferedReader 中。阅读完后我应该关闭这两个阅读器中的哪一个?有关系吗?
FileReader fr = null;
BufferedReader br = null;
try
{
fr = new FileReader(fileName);
br = new BufferedReader(fr);
// ...
}
finally
{
// should I close fr or br here?
}
当谈到异常安全时,我有点偏执。当 BufferedReader 构造函数抛出异常时会发生什么?它会关闭嵌套阅读器吗?还是保证不抛?
When reading from a text file, one typically creates a FileReader
and then nests that in a BufferedReader
. Which of the two readers should I close when I'm done reading? Does it matter?
FileReader fr = null;
BufferedReader br = null;
try
{
fr = new FileReader(fileName);
br = new BufferedReader(fr);
// ...
}
finally
{
// should I close fr or br here?
}
I'm a little paranoid when it comes to exception-safety. What happens when the BufferedReader
constructor throws an exception? Does it close the nested reader? Or is it guaranteed not to throw?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,最外层流包装器上的
close()
会调用包装流上的close()
。但是,如果您认为构造函数可能会引发异常,请自由使用 可关闭界面。因此,即使 JVM 耗尽了缓冲区的堆空间并抛出错误,也不会泄漏文件句柄。
对于 Java 7 及更高版本,请使用 try-with-resources:
Generally,
close()
on the outermost stream wrapper will callclose()
on the wrapped streams. However, if you think it's likely that a constructor will throw an exception, make liberal use of the Closeable interface.So, even if the JVM ran out of heap space for the buffer and threw an error, you wouldn't leak a file handle.
For Java 7 and above use try-with-resources:
仅关闭 BufferedReader 就足够了,因为它包装了 FileReader。如果您查看
的 源代码 BufferedReader
您将看到close
方法关闭了包装的流。Closing only the
BufferedReader
is enough, cause it wraps theFileReader
. If you look at the source code ofBufferedReader
you will see that theclose
method, closes the wrapped stream.在finally 块中关闭BufferedReader。
Close the BufferedReader in a finally block.
如果调用 BufferedReader 的 close 方法,BufferedReader 将调用 FileReader 的 close 方法。因此,两个 close 方法都会被调用。更准确地说,BufferedReader 除了调用 FileReader 的 close 方法之外什么也不做。因此,这根本不重要。尽管我认为调用 BufferedReader 的 close 方法也是一个很好的做法。
If you call the BufferedReader's close method, the BufferedReader will call the FileReader's close method. Thus both close method's are called. More precisely the BufferedReader will do nothing BUT calling the FileReader's close method. Thus it does not matter at all. Though I think it is good practice too call the BufferedReader's close method.
没有什么可以保证不扔。因为缓冲区已分配,所以可能会抛出 OutOfMemoryError。我通常将代码分为两部分:获取资源,然后使用资源。每个部分通常都有独特的清理需求
,下面是用于说明的代码:
我同意你的观点,没有什么比在长时间运行的服务器应用程序中默默地释放文件处理程序更值得的了。
Nothing is guaranteed not to throw. Because the buffer is allocated it may throw OutOfMemoryError. I usually separate my code into 2 sections: acquire resources and then use resources. Each section usually has unique cleanup needs
Here is the code to illustrate:
And I agree with you, there is nothing worth than silently loose a file handler in the long running server application.