java 我需要用 try/catch/finally 块包围 fileInputStream.close 吗?它是如何完成的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11622348/
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
do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?
提问by JoJo
I have the following Java Class that does one thing, fires out values from config.properties
.
我有以下 Java 类,它只做一件事,从config.properties
.
When it comes time to close the fileInputStream
, I think I read on Wikipedia that it is good to have it in a finally block. Because it honestly works just fine in try/catch block.
当需要关闭fileInputStream
. 因为老实说它在 try/catch 块中工作得很好。
Can you show me correction to get fileInputStream.close()
in a finally section?
你能告诉我更正以进入fileInputStream.close()
最后的部分吗?
ConfigProperties.java package base;
ConfigProperties.java 包基础;
import java.io.FileInputStream;
import java.util.Properties;
public class ConfigProperties {
public FileInputStream fileInputStream;
public String property;
public String getConfigProperties(String strProperty) {
Properties configProperties = new Properties();
try {
fileInputStream = new FileInputStream("resources/config.properties");
configProperties.load(fileInputStream);
property = configProperties.getProperty(strProperty);
System.out.println("getConfigProperties(" + strProperty + ")");
// use a finally block to close your Stream.
// If an exception occurs, do you want the application to shut down?
} catch (Exception ex) {
// TODO
System.out.println("Exception: " + ex);
}
finally {
fileInputStream.close();
}
return property;
}
}
Is the solution only to do as Eclipse suggests and do this in the finally block?
解决方案是否只能按照 Eclipse 的建议执行并在 finally 块中执行此操作?
finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
回答by Jeffrey
Yes, that is the common pre-Java 7 solution. However, with the introduction of Java 7, there are now try
-with-resourcestatements which will automatically close any declared resources when the try
block exits:
是的,这是 Java 7 之前的常见解决方案。但是,随着 Java 7 的引入,现在有了try
-with-resource语句,它会在try
块退出时自动关闭任何声明的资源:
try (FileInputStream fileIn = ...) {
// do something
} // fileIn is closed
catch (IOException e) {
//handle exception
}
回答by Jon Lin
Because FileInputStream.close()
throws an IOException, and the finally{} block doesn't catch exceptions. So you need to either catch it or declare it in order to compile. Eclipse's suggestion is fine; catch the IOException inside the finally{} block.
因为FileInputStream.close()
抛出一个 IOException,而 finally{} 块不会捕获异常。因此,您需要捕获它或声明它以进行编译。Eclipse 的建议很好;在 finally{} 块中捕获 IOException。
回答by Bohemian
The standard approach is:
标准方法是:
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(...);
// do something with the inputstream
} catch (IOException e) {
// handle an exception
} finally { // finally blocks are guaranteed to be executed
// close() can throw an IOException too, so we got to wrap that too
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
// handle an exception, or often we just ignore it
}
}