This used to be done like this:
FileOutputStream fos = null;
try {
fos = new FileOutputStream("test.txt");
// do something cool with fos
} catch (FileNotFoundException e) {
// catch code
} catch (IOException e) {
// catch code
} finally {
try {
fos.close();
} catch (Exception ex) {
// log the exception
}
}
Now, if you are using Java 7, you just would have to write this code:
try (FileOutputStream fos = new FileOutputStream("test.txt")) {
// do something cool with fos
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Realize that the recource declaration have to be written on the new try() and that you have to use a resource that implements java.lang.AutoClosable interface.
Best regards!