[post-views]
In this lesson, you will learn.
A stream must be closed when it is no longer needed. Failure to do so can lead to memory leaks and resource starvation.

The first is to call a close( ) function on the stream explicitly.
try {
// open and access file
} catch( I/O-exception) {
// ...
} finally {
// close the file
}
try (resource-specification) {
// use the resource
}
catch (ExceptionType e) {
// handle any exceptions
}
Here, resource-specification is a statement or statement that declares and initializes a resource, such as a file or other stream-related resource.
When the try block ends, the resource is automatically released. In the case of a file, this means that the file is automatically closed. Thus, there is no need to call close( ) explicitly.
Here are the 3 key points regarding try-with resources example
The principal advantage of try-with-resources is that the resource (in this case, a stream) is closed automatically when the try block ends.
The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable or java.io.Closeable can be used as a resource.
Syntax
try (ResourceType resource = new Resource()) {
// use the resource
} catch (ExceptionType e) {
// handle any exceptions
}
After the try block, the resource will be automatically closed, regardless of whether the try block completes normally or abruptly (due to an exception).
This automatic closure eliminates the need for a finally block solely to close the resources, which reduces boilerplate code and improves readability.
package trywithresource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class TraditionalFileWrite {
public static void main(String[] args) {
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter("traditional.txt"));
br.write("It is a traditional approach");
br.write("Make sure to close resources in the finally block.");
System.out.println("File writing operation completes..");
} catch (IOException e) {
System.err.println("Error occurred: " + e.getMessage());
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.err.println("Faile to close: " + e.getMessage());
}
}
}
}
}
Output
package trywithresource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
// BufferedWriter is automatically closed at the end of the try block
try (BufferedWriter br = new BufferedWriter
(new FileWriter("with_resources.txt"))) {
br.write("Hello, this is a test using try-with-resources.n");
br.write("No need for a finally block to close resources.");
System.out.println("File writing operation completes..");
} catch (IOException e) {
System.err.println("Error occurred: " + e.getMessage());
}
// No need for a finally block here
}
}
Output
Traditional Method:
finally block, which adds to the code complexity.finally block can lead to resource leaks, especially if the closure itself can throw an exception.
Try-With-Resources Method:
In summary, try-with-resources provides a safer and cleaner way to handle resources in Java. It is especially useful in scenarios involving closeable resources like streams, where managing closure and exceptions can become cumbersome with traditional try–catch–finally blocks.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.