Automatic Resource Management in Java

This is also stated as try-with-resources in Java. Both of these things are same. It was introduced in Java 7.
In general practice whatever resources like file streams or database connections we are opening in try block we are closing them in respective manner in finally block. If we do not follow this practice then it will lead to memory leak.
Let's take an example to read a file in Java.


package com.infybuzz.file;

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

public class FileReaderDemo2 {

 public static void main(String[] args) {
  
  FileReader reader = null;
  
  BufferedReader br = null;
  
  try {
   
   reader = new FileReader("D:\\hello.txt");
   
   br = new BufferedReader(reader);
   
   String str = "";
   StringBuilder stringBuilder = new StringBuilder();
   
   //reading file line by line
   
   while((str = br.readLine()) != null){
    stringBuilder.append(str).append("\n");
   }
   
   System.out.println(stringBuilder.toString());
   
  }catch (IOException ex) {
   try {
    
    if(br != null) {
     br.close();
    }
    
    if(reader != null) {
     reader.close();
    }
    
   } catch(IOException e) {
    e.printStackTrace();
   }
   
   ex.printStackTrace();
  }
 }

}

Output

Hello
infybuzz
java

So here it is reading a file using FileReader and BufferedReader. So we are calling close() methods inside finally block. Here we need to follow order i.e we first need to close BufferedReader and then FileReader. So this we have to do manually.
In Java 7 new feature has been introduced and that is Automatic Resource Management ( try-with-resources ).
In this we do not need to close anything inside finally. So if you are using finally block to just close these then even you do not need finally block but that's up to you.
Now lets see same above example with Automatic Resource Management.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.infybuzz.file;

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

public class FileReaderDemo3 {

 public static void main(String[] args) {
  
  try (FileReader reader = new FileReader("D:\\hello.txt");
    BufferedReader br = new BufferedReader(reader)){
   
   String str = "";
   StringBuilder stringBuilder = new StringBuilder();
   
   //reading file line by line
   
   while((str = br.readLine()) != null){
    stringBuilder.append(str).append("\n");
   }
   
   System.out.println(stringBuilder.toString());
   
  }catch (IOException ex) {
   ex.printStackTrace();
  }
 }

}

If you see above code then you get idea that we do not have finally block now. Also we have created objects of FileReader and BufferedFileReader along with try as you can see on line 11 and 12.
When multiple resources are opened in try-with-resources, it closes them in the reverse order to avoid any dependency issue.
Java 7 has introduced a new interface java.lang.AutoCloseable. To use any resource in try-with-resources, it must implement AutoCloseable interface else java compiler will throw compilation error.

How to read file in Java using BufferedReader ?
How to write file in Java ?



0 Comments