Friday, September 21, 2012

try-with-resources



Java 1.7 introduced try-with-resources statement: It uses objects that implement the java.lang.AutoCloseable interface and hence you don’t have to use finally block to close the resources.

try (RandomAccessFile in = new RandomAccessFile("file.ran", "r"))
        {           
            String s = in.readLine();
            return s;
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found");
            System.out.println(e.toString());
            return null;           
        }
        catch (IOException e)
        {
            System.out.println("I/o error occurred");
            System.out.println(e.toString());
            return null;
        }

No comments:

Post a Comment