...
Code Block | ||
---|---|---|
| ||
public class Operation { public static void doOperation(String some_file) { // ... code to check or set character encoding ... try { BufferedReader reader = new BufferedReader(new FileReader(some_file)); try { // Do operations } finally { try { reader.close(); } catch (IOException ie) { // Forward to handler } // ... Other clean-up code ... } } catch (IOException x) { // Forward to handler } } } |
Compliant Solution (Java
...
SE 7: try-with-resources)
Java 1.SE 7 introduced a new feature, called try-with-resources, that can close certain resources automatically in the event of an error. This compliant solution uses try-with-resources to properly close the file.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="19df4a24d34e4ac4-63a94494-4ce94acd-9e31a3f9-4bad53acf1f58a5064b13ade"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. References#Bloch 05]] | Puzzle 41. Field and Stream | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="18f65df8390137ce-28d6a925-4d5b45aa-8abfaf41-f325c61cceb5a116e8b6c18f"><ac:plain-text-body><![CDATA[ | [[Chess 2007 | AA. References#Chess 07]] | 8.3, Preventing Resource Leaks (Java) | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1cfe747d8a1d269c-d8fb133f-47554e72-96b89a05-906dc198edd1cc2a9843bd4c"><ac:plain-text-body><![CDATA[ | [[Harold 1999 | AA. References#Harold 99]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="eba2f4914690e221-53ad329d-43c24a4c-820b9562-05a653a8763a5efa462d66c5"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. References#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...