How to close an InputStream file while reading a property file

I have the 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"); } 

Do I need to close FileInputStream? If so, how do I do this? I get a bad practice error in my code checklist. By asking him to block completely.

+7
source share
4 answers

You must close the FileInputStream as there will be no instance of Properties . From Properties.load() javadoc:

The specified stream remains open after this method returns.

Save FileInputStream in a separate variable declared outside of try , and add a finally block that closes FileInputStream if it was open:

 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:

 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"); } 
+8
source

You should always close your threads, and doing this in a finally block is good practice. The reason for this is that the finally block is always executed, and you want to make sure that the thread is always closed, even if something bad happens.

  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 use Java 7, it becomes a lot easier since the new try-with syntax has been introduced. 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 closes automatically.

+2
source

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; } } 
+1
source

You can use Lombok @Cleanup to make it simple. 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 you use Java 7, is there a new "try with a resource" feature. 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"); } 
+1
source

All Articles