- The try-with-resources is try statement that used to declare one or more resources.
- e.g. resources like BufferedReader, Scanner, BufferedWriter can be used with try-with-resource statement.
- Any resource class implementing AutoCloseable interface, can be used with try-with-resource statement.
- Resources are closed automatically at the end of try statement.
- We will look into the resources implementing AutoCloseable interface e.g. BufferedWriter, BufferedReader.
- We will discuss Class hierarchy of custom BufferedWriter & BufferedReader classed with the help of example program.
Examples of AutoCloseable Interfaces:
1. BufferedReader class Implements AutoCloseable Interface:
- BufferedReader class implements AutoCloseable.
- BufferedReader class can be used with try-with-resource statement.
- We will create our custom BufferedReader class by extending BufferedReader class, to demonstrate close method of AutoCloseable interface.
- Override close method in CustomBufferedReader class.
- close method will be called automatically at the end of try statement.
- Class hierarchy of custom BufferedReader class is as follows:
Program – CustomBufferedReader class code is as follows:
class CustomBufferedReader extends BufferedReader {
public CustomBufferedReader(Reader in) {
super(in);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("AutoCloseable: Closing BufferReader resource");
}
}
2. BufferedWriter class implements AutoCloseable interface:
- BufferedWriter class implements AutoCloseable interface (similar to BufferedReader class)
- BufferedWriter class can be used with try-with-resource statement.
- Create CustomBufferWriter class by extending BufferedWriter class.
- Override close method in CustomBufferedWriter class.
- Class hierarchy of custom bufferedWriter class is as follows:
Program – CustomBufferedWriter class code is as follows:
class CustomBufferedWriter extends BufferedWriter{
public CustomBufferedWriter(Writer out) {
super(out);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("AutoCloseable: Closing CustomBufferedWriter resource");
}
}
AutoCloseable interface:
- Resources implementing AutoCloseable interface will be closed automatically (on exiting the try statement).
- Custom BufferedReader & BufferedWriter class implements AutoCloseable interface.
- Let us look into the complete code to demonstrate AutoCloseable interface.
- close method of CustomBufferedReader & CustomBufferedWriter class will be called automatically.
Program of Custom BufferedReader & BufferedWriter Classes
package org.learn.try_with_resource;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class DemoTryWithResource
{
public static void main( String[] args ) throws IOException
{
writeUsingBufferWriter();
readUsingBufferReader();
}
private static void writeUsingBufferWriter() throws IOException {
System.out.println("1. Start writing contents to file - CustomBufferedWriter");
try (FileOutputStream fileStream = new FileOutputStream(new File("sampleFile.txt"));
OutputStreamWriter writer = new OutputStreamWriter(fileStream);
CustomBufferedWriter customBufferWriter = new CustomBufferedWriter(writer)) {
customBufferWriter.write("Soccer");
customBufferWriter.newLine();
customBufferWriter.write("Tennis");
}
System.out.println("3. Successfully written contents to file - CustomBufferedWriter");
}
private static void readUsingBufferReader() throws IOException {
System.out.println("\n4. Start Reading file using CustomBufferedReader");
try (FileInputStream fileStream = new FileInputStream(new File("sampleFile.txt"));
InputStreamReader reader = new InputStreamReader(fileStream);
CustomBufferedReader customBufferedReader = new CustomBufferedReader(reader)) {
String line;
while ((line = customBufferedReader.readLine()) != null) {
System.out.println(line);
}
}
System.out.println("6. End Reading file using CustomBufferedReader");
}
}
class CustomBufferedWriter extends BufferedWriter{
public CustomBufferedWriter(Writer out) {
super(out);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("2. AutoCloseable: Closing CustomBufferedWriter resource");
}
}
class CustomBufferedReader extends BufferedReader {
public CustomBufferedReader(Reader in) {
super(in);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("5. AutoCloseable: Closing CustomBufferedReader resource");
}
}
Output of AutoCloseable & try-with-resource is as follows:
1. Start writing contents to file - CustomBufferedWriter
2. AutoCloseable: Closing CustomBufferedWriter resource
3. Successfully written contents to file - CustomBufferedWriter
4. Start Reading file using CustomBufferedReader
Soccer
Tennis
5. AutoCloseable: Closing CustomBufferedReader resource
6. End Reading file using CustomBufferedReader