Site icon

try with resources & AutoCloseable java 7 (example & class hierarchy)

Examples of AutoCloseable Interfaces:

1. BufferedReader class Implements AutoCloseable Interface:

Fig 1: CustomBufferedReader class hierarchy

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:

Fig 2: CustomBufferedWriter class hierarchy

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:

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
Exit mobile version