Thursday, May 1, 2014

Java vs. C# - Resource Auto Closing

       

            static String readFirstLineFromFile(String path) throws IOException {
              try (BufferedReader br =
                          new BufferedReader(new FileReader(path))) {
                      return br.readLine();
              }
            }
       
 
The above is similar and appears to have the same use case as the C# use statement, in that it allows you to omit the finally block so long as the resource in question implements java.lang.AutoCloseable (Java) vs IDisposeable .NET. The above was new in Java SE 7. ref: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

No comments:

Post a Comment