What is ARM in Java?


A resource is an object which implements AutoClosable interface. whenever you use a resource in your program it is recommended to close it after the usage.

Initially, this task is done using the finally block.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class FinalExample {
   public static void main(String[] args) throws IOException {
      File file = null;
      FileInputStream inputStream = null;
      try {
         file = new File("D:\\source\\sample.txt");
         inputStream = new FileInputStream(file);
         Scanner sc = new Scanner(inputStream);
         while(sc.hasNextLine()) {
            System.out.println(sc.nextLine());
         }
      } catch(IOException ioe) {
         ioe.printStackTrace();
      } finally {
         inputStream.close();
      }
   }
}

Output

This is a sample file with sample text

ARM

ARM in Java stands for automatic resource management, it was introduced in Java7, in it the resources should be declared at the try block and they will be closed automatically at the end of the block. It is also known as try-with resources block and the objects we declare in this should be a resource i.e. they should be of the type AutoClosable.

Following is the syntax of try-with-resources statement −

try(ClassName obj = new ClassName()){
   //code……
}

From JSE7 onwards the try-with-resources statement is introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)

The resources we declare in the try block should extend the java.lang.AutoCloseable class.

Example

Following program demonstrates the try-with-resources in Java.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class FinalExample {
   public static void main(String[] args) throws IOException {
      try(FileInputStream inputStream = new FileInputStream(new File("D:\\source\\sample.txt"));) {
         Scanner sc = new Scanner(inputStream);
         while(sc.hasNextLine()) {
            System.out.println(sc.nextLine());
         }
      } catch(IOException ioe) {
         ioe.printStackTrace();
      }
   }
}

Output

This is a sample file with sample text

Multiple resources in Java

You can also declare multiple resources in try-with resources and they all will be closed at once at the end of the block.

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopying {
   public static void main(String[] args) {
      try(FileInputStream inS = new FileInputStream(new File("E:\\Test\\sample.txt"));
      FileOutputStream outS = new FileOutputStream(new File("E:\\Test\\duplicate.txt"))){
         byte[] buffer = new byte[1024];
         int length;
         while ((length = inS.read(buffer)) > 0) {
            outS.write(buffer, 0, length);
         }
         System.out.println("File copied successfully!!");
      } catch(IOException ioe) {
         ioe.printStackTrace();
      }
   }
}

Output

File copied successfully!!
raja
Published on 10-Oct-2019 10:21:57
Advertisements