在 Java 中关闭 OutputStream 的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6269078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:07:11  来源:igfitidea点击:

Proper way to close an OutputStream in Java?

java

提问by Andrew White

This almost seems silly but what is the most reliable pattern to follow when closing an OutputStream? Right now I have something like the following which just seem to be try-catch-finally-overkill:

这几乎看起来很愚蠢,但是关闭 OutputStream 时要遵循的最可靠模式是什么?现在我有类似以下的东西,这似乎是 try-catch-finally-overkill:

private void writeContentsToFile(OutputStream ostream, Properties contents) {
    try {
        contents.store(ostream, "comments");
    }
    catch (IOException e) {
        throw new ResourceException("Failed to write contents", e);
    }
    finally {
        try {
            ostream.close();
        }
        catch (IOException e) { /* what can be done here anyway? */ }
    }
}

Why close throws a checked exception is still a mystery to me. I can create wrapper method that does the close/catch block but if there is something already out there like FileUtil.closeFileAndThrowUncheckedException()I would like to use it. This gets a bit more useful when you have lots of smaller projects with lots of devs; one way to do it right.

为什么 close 会抛出受检异常对我来说仍然是个谜。我可以创建执行 close/catch 块的包装器方法,但是如果已经有一些东西像FileUtil.closeFileAndThrowUncheckedException()我想使用它一样。当你有很多有很多开发人员的小项目时,这会更有用;一种正确的方法。

采纳答案by Sripathi Krishnan

If you are using Apache Commons, then IOUtils.closeQuietly() does the job nicely. See http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.OutputStream)

如果您使用的是 Apache Commons,那么 IOUtils.closeQuietly() 可以很好地完成这项工作。见http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.OutputStream)

回答by irreputable

I don't think the exception should be swallowed. The caller is misled to believe that the content is written successfully. The exception should propagate upwards.

我认为不应该吞下这个例外。调用者被误导认为内容写入成功。异常应该向上传播。

If you don't like checked exceptions (especially for such low level errors), wrap it as unchecked. Or you can follow Java's convention, for better or for worse, and declare IOException on your method.

如果您不喜欢已检查的异常(尤其是对于此类低级错误),请将其包装为未检查的。或者您可以遵循 Java 的约定,无论好坏,并在您的方法上声明 IOException。

回答by Vincent Koeman

I think your way is the 'best' way. If close throws an exception, you indeed just cannot do anything about it. This probably throws an caught exception because it might be bad, depending on how it's used. If you really need to close a file you would like to enforce it. If you just want to use close for errorhandling, you should just ignore the exception.

我认为你的方式是“最好的”方式。如果 close 抛出异常,您确实无法对此做任何事情。这可能会抛出一个捕获的异常,因为它可能很糟糕,这取决于它的使用方式。如果您确实需要关闭一个文件,您希望强制执行它。如果您只想使用 close 进行错误处理,您应该忽略异常。