...
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 cleanup code ... } } catch (IOException x) { // Forward to handler } } } |
Compliant Solution (
...
try
-with-resources)
Java SE 7 introduced a new feature , called try
-with-resources, that that can close certain resources automatically in the event of an error. This compliant solution uses try
-with-resources to properly close the file.
...