- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Automatic resource management in Javan
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.
- Related Articles
- Operating System Resource Management
- What is Human Resource Management?
- PMP® – Resource Management Maturity Model
- Human Resource Management: Challenges & Solutions?
- What are automatic modules in Java 9?
- What is maven automatic build tool in Java eclipse projects?
- How does effective Human resource management help in your company's growth?
- What is try-with-resource in java?
- Memory management in Java
- Any good resource for java interview questions
- Automatic Repeat reQuest (ARQ)
- Automatic Power Save Delivery (APSD)
- How to get Resource name using Resource id in Android?
- How to get Resource Name using Resource id in Android using Kotlin?
- Automatic detection of display availability with Matplotlib
