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.
You must the close the
FileInputStream, as thePropertiesinstance will not. From theProperties.load()javadoc:Store the
FileInputStreamin a separate variable, declared outside of thetryand add afinallyblock that closes theFileInputStreamif it was opened:Use try-with-resources since Java 7: