java 读取属性文件时如何关闭fileInputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11430289/
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
how to close the fileInputStream while reading the property file
提问by Beginner
I have following code :
我有以下代码:
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
}catch (IOException e) {
system.out.println("IOEXCeption");
}
Is it required to close the FileInputStream? If yes, how do I do that? I am getting a bad practice error in my code checklist . Asking it to put finally block.
是否需要关闭 FileInputStream?如果是,我该怎么做?我的代码清单中出现了错误的做法错误。要求它把 finally 块。
回答by hmjd
You must the close the FileInputStream
, as the Properties
instance will not. From the Properties.load()
javadoc:
您必须关闭FileInputStream
,因为Properties
实例不会。从Properties.load()
javadoc:
The specified stream remains open after this method returns.
此方法返回后,指定的流保持打开状态。
Store the FileInputStream
in a separate variable, declared outside of the try
and add a finally
block that closes the FileInputStream
if it was opened:
将 the 存储FileInputStream
在一个单独的变量中,在 the 之外声明try
并添加一个finally
块,FileInputStream
如果它被打开,则关闭它:
Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream("filename.properties");
properties.load(fis);
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
} catch (IOException e) {
system.out.println("IOEXCeption");
} finally {
if (null != fis)
{
try
{
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Use try-with-resourcessince Java 7:
从 Java 7 开始使用try-with-resources:
final Properties properties = new Properties();
try (final FileInputStream fis =
new FileInputStream("filename.properties"))
{
properties.load(fis);
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
} catch (IOException e) {
system.out.println("IOEXCeption");
}
回答by Keppil
You should always close your streams, and doing it in the finally
block is a good practice. The reason for this is that the finally
block always gets executed, and you want to make sure that the stream is always closed, even if Something Bad happens.
你应该总是关闭你的流,在finally
块中这样做是一个很好的做法。这样做的原因是该finally
块总是被执行,并且您希望确保流始终关闭,即使发生了一些不好的事情。
FileInputStream inStream = null;
try {
inStream = new FileInputStream("filename.properties");
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
} finally {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
If you are using Java 7, this becomes much easier, since a new try-with
syntax was introduced. Then you can write like this:
如果您使用的是 Java 7,这将变得更加容易,因为try-with
引入了新的语法。然后你可以这样写:
try(FileInputStream inStream = new FileInputStream("filename.properties")){
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
}
and the stream is closed automatically.
并且流会自动关闭。
回答by M. Abbas
here is an example:
这是一个例子:
public class PropertiesHelper {
public static Properties loadFromFile(String file) throws IOException {
Properties properties = new Properties();
FileInputStream stream = new FileInputStream(file);
try {
properties.load(stream);
} finally {
stream.close();
}
return properties;
}
}
回答by Sylvain
You can use Lombok @Cleanup to do it simply. http://projectlombok.org/features/Cleanup.html
您可以使用 Lombok @Cleanup 来简单地做到这一点。 http://projectlombok.org/features/Cleanup.html
Properties properties = new Properties();
try {
@Cleanup FileInputStream myFis = new FileInputStream("filename.properties");
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}
Or, only if your are using Java 7, there is the "try with resource" new feature. http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
或者,仅当您使用 Java 7 时,才有“尝试资源”新功能。 http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Properties properties = new Properties();
try (FileInputStream myFis = new FileInputStream("filename.properties")) {
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}