[英]how to close the fileInputStream while reading the property file
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? 是否需要关闭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.
要求它最终阻止。
You must the close the FileInputStream
, as the Properties
instance will not. 您必须关闭
FileInputStream
,因为Properties
实例不会。 From the Properties.load()
javadoc: 从
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: 将
FileInputStream
存储在一个单独的变量中,在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-resources since 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");
}
You should always close your streams, and doing it in the finally
block is a good practice. 你应该总是关闭你的流,并在
finally
块中执行它是一个很好的做法。 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
块总是被执行,并且您希望确保流始终关闭,即使发生了Something Bad。
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. 如果您使用的是Java 7,则会变得更加容易,因为引入了新的
try-with
语法。 Then you can write like this: 然后你可以像这样写:
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. 并自动关闭流。
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;
}
}
You can use Lombok @Cleanup to do it simply. 你可以使用Lombok @Cleanup来做到这一点。 http://projectlombok.org/features/Cleanup.html
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. 或者,只有当您使用Java 7时,才有“尝试使用资源”的新功能。 http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.