Java Try-with-Resources was first introduced in Java 7 to manage the resource automatically. The try-with-resources statement received a major change in Java 9 which simplifies the code. In this post, we will discuss the what is try-with-resource statement and the improvements made to it in Java 9.
What is Java Try with Resources statement?
Before getting to the improvements made to the Try with Resources statement, let’s understand what is try with resources statement ?
The try-with-resources statement can define one or more resources which must be closed at the end of the program execution. The try-with-resources block ensures that each resource is closed at the end of the execution. Any object that implements java.lang.AutoCloseable can be passed as a resource.
Before Java 7
Before Java 7, while reading a file we need to close the Readers manually inside the finally block.
In the below code we are reading a file in Java, we have two resources FileReader and BufferedReader which is closed in the finally block. Which is a boilerplate which we needs to be handled by the developer.
package com.javainterviewpoint; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaReader { public static void main(String[] args) throws IOException { FileReader fileReader = null; BufferedReader bufferedReader = null; try { fileReader = new FileReader("E:\\jip\\test.txt"); bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); if (fileReader != null) fileReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
After Java 7
After the introduction of try-with-resources statement in Java 7 there is no necessity to use of finally block. The required resources will be opened inside the try brackets and will be auto closed regardless of whether the program completes normally or terminates abruptly.
package com.javainterviewpoint; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaReader { public static void main(String[] args) throws IOException { try (BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\jip\\test.txt"))) { String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
In the above we can see that we have created the instance of the FileReader and BufferedReader inside the try and we have not used the finally block to close the opened resources as it will be closed automatically by the try-with-resources statement.
Java 9 try-with-resources Statement
Issue with Java 7 try-with-resources – Resource declared outside
However there is a small issue which occurs with the try-with-resources statement of Java 7, it will not allow the resources which is created outside the try block. It throws the compilation error.
Let’s take a look into the below code.
We have created the BufferedReader outside the try block and have passed the reference alone to the try.
package com.javainterviewpoint; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaReader { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\jip\\test.txt")); try (bufferedReader) { String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
Output:
Exception in thread "main" java.lang.Error: Unresolved
compilation problems:
Syntax error, insert "VariableDeclaratorId = VariableInitializer"
to complete Resources
bufferedReader cannot be resolved to a type
at com.javainterviewpoint.JavaReader.main(JavaReader.java:13)
Workaround in Java 7
In order to overcome above issue in Java 7, we have to do a workaround. We have to create a local reference in the try and the code will execute properly.
In the below code we have refereed the bufferedReader with the local variable newBufferedReader within the try
package com.javainterviewpoint;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class JavaReader
{
public static void main(String[] args) throws IOException
{
BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\jip\\test.txt"));
try (BufferedReader newBufferedReader = bufferedReader)
{
String line;
while ((line = newBufferedReader.readLine()) != null)
{
System.out.println(line);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
This issues has been addressed in Java 9, now try-with-resources Statement allows you to declare the resource outside and no need to create a local reference to access the resource. The code which failed in Java 7 would run perfectly in Java 9.
package com.javainterviewpoint; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class JavaReader { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader("E:\\jip\\test.txt")); try (bufferedReader) { String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
Output:
Welcome to JavaInterviewPoint!!!
Leave a Reply