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.
Keppil
source share