Java 7: New Feature – Automatically Close Files and Resources in try-catch-finally
Join the DZone community and get the full member experience.
Join For FreeTry with resources is a new feature in Java 7 which lets us write more elegant code by automatically closing resources like FileInputStream at the end of the try-block.
Old Try Catch Finally
Dealing with resources like InputStreams is painful when it comes to the try-catch-finally blocks. You need to declare the resources outside the try so that they are is accessible from finally, then you must initialize the variable to null and check for non-null when closing the resource in finally.
Java (programming language)
Opinions expressed by DZone contributors are their own.
Comments