Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Try with Resources/Automatic Resource Management (ARM)

Java Programming Language Try with Resources or Automatic Resource Management

Before JDK 7, we were required to close the files explicitly by using the close() method. But some time programmer may forget to close/free the file/resources which could result in memory leakage.

Therefore to prevent memory leakage due to unclosed resources, Java introduced the new feature known as Automatic Resource Management (ARM) from JDK 7.

Automatic Resource Management (ARM) makes all the resources free automatically, that is without calling close() method explicitly.

To free the resources automatically, Automatic Resource Management (ARM) uses the new form of try statement known as a trywithresource statement.

Syntax of try-with-resource statement

 try(setting resources)
{
    // use of resources
}

Inside left & right parenthesis, declaration & initialization of resources is done.
Within { } braces resources are used.

The moment try block ends, all the resources are closed automatically without using finally block & calling close() method explicitly.

Note

All the resources used with try-with-resource statement must implement the AutoCloseable interface.
AutoCloseable interface defines only one method called close().

Example without try-with-resources statement

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class WithoutTWR 
{
    public static void main(String args[])
    {
        try
        {
            FileReader f = new FileReader("NewFile.txt");
            BufferedReader br = new BufferedReader(f);
            String str = br.readLine();
            while(str!=null)
            {
                System.out.println(str);
                str = br.readLine();
            }
            br.close(); // resource closed explicitly
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Example with try-with-resources statement

No need to call close() method explicitly to close the resource.

When try statement ends, the resource is automatically closed using implicit call to close() method.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class WithTWR 
{
    public static void main(String args[])
    {
        try(BufferedReader br = new BufferedReader(new FileReader("NewFile.txt")))
        {
            String str = br.readLine();
            while(str!=null)
            {
                System.out.println(str);
                str = br.readLine();
            }
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }
}

Note

In a try-with-resources statement, all the resource mentioned in a try statement is implicitly final. Therefore we can’t reassign any resource after its creation.

Example

 try(BufferedReader br = new BufferedReader(new FileReader("NewFile.txt")))
 {
     br = new BufferedReader(new FileReader("NewFile2.txt")); // complile time error
 }
 catch(IOException e)
 {
     System.out.println(e);
 }

This code will give compile time error

               The resource br of a try-with-resources statement cannot be assigned

Multiple resources within a single try statement

We can declare more than one resource in a single try statement, just by separating these resources with a semicolon.

Syntax

 try(resource1; resource2;------; resourceN)
{
    // using the resources
}

Example

try(BufferedWriter bw = new BufferedWriter(new FileWriter("NewFile.txt"));
		BufferedReader br = new BufferedReader(new FileReader("NewFile.txt")))
{
            
}
catch(IOException e)
{
    System.out.println(e);
}

 

Leave a reply