Use Java Try with Resources | Examples | SpringJava

Jiledar_Dhakad
3 min readMar 15, 2023

Try with Resouces feature introduce in Java 1.7 version. Prior to Java 1.7 version, all resources are closed in the finally block. Then resources are closed in the finally block by the programmer explicitly increasing the length of the code, and reducing the readability of the code. So Java resolves this limitation to introduce the try-with-resource statement feature in Java 1.7. In the use of this feature, whatever resources are opened will be closed automatically once the control reaches the end of the try block either normally or abnormally.

Table of content

1. What is the try with resources in Java?
2. How to declare try with resources in Java?
3. What are the advantages of the try with resources in Java?
4. Try with resource examples in Java 8
5. Try with resource enhancement and example in Java 9
6. Conclusion

1. What is the try with resources in Java?

Try with resources used to declare resources in the try block in Java. Resources are objects that are closed implicitly once control reaches the end of the try block of the programs either normally or abnormally.

2. How to declare try with resources in Java?

Syntax of the try-with-resources statement:

try(R r; R r){

}

→ R — It is a resource. It is an object.
→ Any objects that implement java.lang.AutoCloseable which includes all objects which implement java.io.Closeable can be used as a resource in the try block of the programs.

3. What are the advantages of the try with resources in Java?

There are some of the advantages of the try-with-resources in Java are mentioned below:
• Reducing the length of the code of the programs
• Improving the readability of the code of the programs
• There is no need to close resources in the finally block by the programmer explicitly

4. Try with resource examples in Java 8

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class Test {
public static void main(String[] args) {
System.out.println(readMessage("Hello try-with-resource until Java 8"));
}
static String readMessage(String message) {
Reader inputMsg = new StringReader(message);
BufferedReader br = new BufferedReader(inputMsg);
String output = "";
try (BufferedReader br1 = br) {
output = br1.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
}

Output

Hello try-with-resource until Java 8

→ Until Java 8 we used resources in the try block that are local resources reference in that block. We can’t use outside resources in the try-with-resource.

5. Try with resource enhancement and example in Java 9

The try-with-resource feature is enhanced in Java 9.Java 9 onwards we can use an outside resource in try-with-resource. An example of the try-with-resource enhancement feature is below:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class Test {
public static void main(String[] args) {
System.out.println(readMessage("Hello try-with-resource enhancement Java 9"));
}
static String readMessage(String message) {
Reader inputMsg = new StringReader(message);
BufferedReader br = new BufferedReader(inputMsg);
String output = "";
try (br) {
output = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
}

Output

Hello try-with-resource enhancement Java 9

6. Conclusion

In this topic, we learnt about what is try-with-resource in java, the advantages of try-with-resource, and try-with-resource enhancement in Java 9 and examples of try-with-resource.

--

--

Jiledar_Dhakad

Spring Java tutorials provides well contents to learn java, spring framework and also the architecture of spring technology for beginners and professionals.