Automatic Resource Management in Java ( try with resource statements ) - GeeksforGeeks

24 Jan, 2022
Java provides a feature to make the code more robust and to cut down the lines of code. This feature is known as Automatic Resource Management(ARM) using try-with-resources from Java 7 onwards. The try-with-resources statement is a try statement that declares one or more resources. 
This statement ensures that each resource is closed at the end of the statement, which eases working with external resources that need to be disposed of or closed in case of errors or successful completion of a code block.
A resource is an object that must be closed after the program is finished using it. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
In earlier versions of Java before JDK 1.7, the closing of resources was done using the finally block. 
Java
// Java program to illustrate cleaning of// resources before Java 7  import java.io.*;import java.util.*;import java.io.*;class Resource{    public static void main(String args[])    {        BufferedReader br = null;        String str = " ";                  System.out.println("Enter the file path");        br = new BufferedReader(new InputStreamReader(System.in));                  try        {            str = br.readLine();            }        catch(IOException e)        {            e.printStackTrace();        }                  try        {            String s;                          // file resource            br = new BufferedReader(new FileReader(str));                          while ((s = br.readLine()) != null)            {                // print all the lines in the text file                System.out.println(s);            }        }        catch (IOException e)        {            e.printStackTrace();        }                  finally        {            try            {                if (br != null)                                      // closing the resource in 'finally' block                    br.close();            }            catch (IOException ex)            {                ex.printStackTrace();            }        }    }}
Output:
hello
java
In the try-with-resources method, there is no use of the finally block. The file resource is opened in try block inside small brackets. Only the objects of those classes can be opened within the block which implements the AutoCloseable interface, and those objects should also be local. The resource will be closed automatically regardless of whether the try statement completes normally or abruptly. 
Syntax: 
The following example reads the first line from a file. It uses an instance of BufferedReader to read data from the file. BufferedReader is a resource that must be closed after the program is finished with it: 
static String readFirstLineFromFile(String path) throws IOException
{
    try (BufferedReader br = new BufferedReader(new FileReader(path)))
    {
        return br.readLine();
    }
}
Java
// Java program to illustrate// Automatic Resource Management// in Java without finally block  import java.io.*;import java.util.*;  class Resource {    public static void main(String args[])    {        String str = "";        BufferedReader br = null;          System.out.println("Enter the file path");        br = new BufferedReader(            new InputStreamReader(System.in));          try {            str = br.readLine();        }        catch (IOException e) {            e.printStackTrace();        }          // try with Resource        // note the syntax difference        try (BufferedReader b             = new BufferedReader(new FileReader(str))) {            String s;            while ((s = b.readLine()) != null) {                System.out.println(s);            }        }        catch (IOException e) {            e.printStackTrace();        }    }}
Output:
hello 
java
Multiple resources can be used inside a try-with-resources block and have them all automatically closed. In this case, the resources will be closed in the reverse order in which they were created inside the brackets. 
Java
// Java program to illustrate// Automatic Resource Management// in Java with multiple resource  class Resource {    public static void main(String s[])    {        // note the order of opening the resources        try (Demo d = new Demo(); Demo1 d1 = new Demo1()) {            int x = 10 / 0;            d.show();            d1.show1();        }        catch (ArithmeticException e) {            System.out.println(e);        }    }}  // custom resource 1class Demo implements AutoCloseable {    void show() { System.out.println("inside show"); }    public void close()    {        System.out.println("close from demo");    }}  // custom resource 2class Demo1 implements AutoCloseable {    void show1() { System.out.println("inside show1"); }    public void close()    {        System.out.println("close from demo1");    }}
Output:
close from demo1
close from demo
Note: In the above example, Demo and Demo1 are the custom resources managed inside the try block. Such resources need to implement the AutoCloseable interface. When we open any such AutoCloseable resource in the special try-with-resource block, immediately after finishing the try block, JVM calls this.close() method on all resources initialized in the try block. 
Important Points: 
The finally blocks were used to clean up the resources before Java 7.After java 7, resource cleanup is done automatically.ARM is done when you initialize resources in the try-with-resources block because of the interface AutoCloseable. Its close method is invoked by JVM as soon as the try block finishes.Calling the close() method might lead to unexpected results.A resource that we use in try-with-resource must be subtypes of AutoCloseable to avoid a compile-time error.The resources which are used in multiple resource ARM must be closed in reverse order as given in the above example
The finally blocks were used to clean up the resources before Java 7.
After java 7, resource cleanup is done automatically.
ARM is done when you initialize resources in the try-with-resources block because of the interface AutoCloseable. Its close method is invoked by JVM as soon as the try block finishes.
Calling the close() method might lead to unexpected results.
A resource that we use in try-with-resource must be subtypes of AutoCloseable to avoid a compile-time error.
The resources which are used in multiple resource ARM must be closed in reverse order as given in the above example
This article is contributed by Apoorva singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
pskamble
nishkarshgandhi
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Interfaces in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Multithreading in Java
Collections in Java
Queue Interface In Java