I have the following Java class that does one thing, it returns values โโfrom config.properties.
When the time comes to close fileInputStream, I think I read on Wikipedia that itโs good to have it in the finally block. Because it honestly works great in a try / catch block.
Can you show me the correction to get fileInputStream.close()at the end of the section?
ConfigProperties.java package database;
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 + ")");
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
finally {
fileInputStream.close();
}
return property;
}
}
Is this solution only what Eclipse offers and do it in a finally block?
finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thanks everyone!
source
share