关闭嵌套 Reader

发布于 2024-10-15 19:16:40 字数 370 浏览 4 评论 0原文

从文本文件读取时,通常会创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哭了丶谁疼 2024-10-22 19:16:40

通常,最外层流包装器上的 close() 会调用包装流上的 close() 。但是,如果您认为构造函数可能会引发异常,请自由使用 可关闭界面。

FileReader fr = new FileReader(fileName);
Closeable res = fr;
try {
    BufferedReader br = new BufferedReader(fr);
    res = br;
} finally {
    res.close();
}

因此,即使 JVM 耗尽了缓冲区的堆空间并抛出错误,也不会泄漏文件句柄。

对于 Java 7 及更高版本,请使用 try-with-resources:

try (FileReader fr = new FileReader(fileName);
    BufferedReader br = new BufferedReader(fr)) {
  // do work
}

Generally, close() on the outermost stream wrapper will call close() 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.

FileReader fr = new FileReader(fileName);
Closeable res = fr;
try {
    BufferedReader br = new BufferedReader(fr);
    res = br;
} finally {
    res.close();
}

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:

try (FileReader fr = new FileReader(fileName);
    BufferedReader br = new BufferedReader(fr)) {
  // do work
}
寒冷纷飞旳雪 2024-10-22 19:16:40

仅关闭 BufferedReader 就足够了,因为它包装了 FileReader。如果您查看 源代码 BufferedReader 您将看到 close 方法关闭了包装的流。

Closing only the BufferedReader is enough, cause it wraps the FileReader. If you look at the source code of BufferedReader you will see that the close method, closes the wrapped stream.

命比纸薄 2024-10-22 19:16:40

在finally 块中关闭BufferedReader。

Close the BufferedReader in a finally block.

花之痕靓丽 2024-10-22 19:16:40

如果调用 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.

随梦而飞# 2024-10-22 19:16:40

没有什么可以保证不扔。因为缓冲区已分配,所以可能会抛出 OutOfMemoryError。我通常将代码分为两部分:获取资源,然后使用资源。每个部分通常都有独特的清理需求

,下面是用于说明的代码:

// Acquire resources section.

final FileReader fr = new FileReader( fileName );

BufferedReader br = null;

try
{
    br = new BufferedReader(fr);
}
finally
{
    if ( br == null )
    {
        // Note that you are closing the fr here
        fr.close( );
    }
}

// Use resources section
try
{
    // ... use br
}
finally
{
    // Now that br is safely constructed, just all its close
    br.close( );
}

我同意你的观点,没有什么比在长时间运行的服务器应用程序中默默地释放文件处理程序更值得的了。

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:

// Acquire resources section.

final FileReader fr = new FileReader( fileName );

BufferedReader br = null;

try
{
    br = new BufferedReader(fr);
}
finally
{
    if ( br == null )
    {
        // Note that you are closing the fr here
        fr.close( );
    }
}

// Use resources section
try
{
    // ... use br
}
finally
{
    // Now that br is safely constructed, just all its close
    br.close( );
}

And I agree with you, there is nothing worth than silently loose a file handler in the long running server application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文