Automatic resource management in Java


automatic resource management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.

Resource

A resource is an object which is required to be closed once our program finishes. For example, a file is read, database connection and so on.

Usage

To use the try-with-resources statement, you simply need to declare the required resources within the parenthesis, and the created resource will be closed automatically at the end of the block. Following is the syntax of the try-with-resources statement.

Syntax

try(FileReader fr = new FileReader("file path")) {    
   // use the resource    
   } catch () {
      // body of catch    
   }
}

Following is the program that reads the data in a file using the try-with-resources statement.

Example

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

public class Try_withDemo {

   public static void main(String args[]) {
      try(FileReader fr = new FileReader("E://file.txt")) {
         char [] a = new char[50];
         fr.read(a);   // reads the contentto the array
         for(char c : a)
         System.out.print(c);   // prints the characters one by one
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Older way of resource management

Prior to Java 7, when we use any resources like streams, connections, etc. we have to close them explicitly using finally block. In the following program, we are reading data from a file using FileReader and we are closing it using finally block.

Example

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadData_Demo {

   public static void main(String args[]) {
      FileReader fr = null;        try {
         File file = new File("file.txt");
         fr = new FileReader(file); char [] a = new char[50];
         fr.read(a);   // reads the content to the array
         for(char c : a)
         System.out.print(c);   // prints the characters one by one
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         try {
            fr.close();
         } catch (IOException ex) {              ex.printStackTrace();
         }
      }
   }
}

Points to remember

Following points are to be kept in mind while working with the try-with-resources statement.

  • To use a class with the try-with-resources statement it should implement the AutoCloseable interface and the close() method of it gets invoked automatically at runtime.

  • You can declare more than one class in a try-with-resources statement.

  • While you declare multiple classes in the try block of try-with-resources statement these classes are closed in reverse order.

  • Except for the declaration of resources within the parenthesis, everything is the same as normal try/catch block of a try block.

  • The resource declared in try gets instantiated just before the start of the try-block.

  • The resource declared at the try block is implicitly declared as final.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 18-Jun-2020

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements