Understanding AutoCloseable and Closeable Interfaces

Lets start with what really resources are!
In java when we refer to term "resources" in a context of java IO operations, we typically mean classes that directly interact with external entities such as files ,streams,sockets etc.
Not all classes in java.io package deals with such external entities and usually those who do get treated as "resources".
The key point here is that all such classes implements either "Autocloseable" or "Closeable" interface.
The Closeable interface extends the Autocloseable interface. Both interfaces define a "close()" method that is intended for automatically closing resources. When resource classes implement either of these interfaces, they can be used within a "try-with-resources" statement, ensuring that the resources are closed automatically at the end of the statement.
However they distinct from each other in a way that their close() method's exception being thrown.Autocloseable's close() method throws "Exception" ,whereas Closeable being specialization of Autocloseable interface, it is IO operation specific so its close() method throws "IOException".
package java.lang;
@FunctionalInterface
public interface AutoCloseable {
void close() throws Exception;
}
package java.io;
import java.io.IOException;
public interface Closeable extends AutoCloseable {
void close() throws IOException;
}
Now understand this, "Exception" class is being partially checked ,it not compulsory to class whoever calling the method to handle it but "IOException" being "Checked" exception, it is mandatory for a class calling this method to handle exceptions raised while closing resources.This leads to standardized way to handle IO exceptions and resources being properly managed and closed.
So from design perspective if your class is a resource dealing with external entities make sure it implements Autocloseable or Closeable interface and from Client programmer's perspective ,if you are using any resources your best bet is to use them via "try with resources" as they will be automatically get close by the end of try block.
If you are planning to close resources manually , make sure to check them if they are indeed closed via null check and close them in plain simple try block.
It becomes such important to close these resources because your class that deals with actual resource which are external entities such as various file handles, network connections, database connections are mostly limited across system,if not properly closed we may face severe problems like,resource exhaustion leading to application failure,resource leaks,loosing data integrity e.g file being partially written or entirely blank and not a written at all.Also leaving resources open lead to potential threat of unauthorized access leading to severe security concerns.
So properly closing resources contribute to overall stability and security of java application , this is the benefit of try with resources as it automatically calls "close()" method of interface implemented by resources.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TryWithResourcesExample {
public static void main(String[] args) {
String someinputfilepath = "input.txt";
String someoutputfilepath = "output.txt";
// Using try-with-resources to ensure resources are closed automatically
try (FileInputStream fis = new FileInputStream(someinputfilepath);
FileOutputStream fos = new FileOutputStream(someoutputfilepath)) {
int byteData;
while ((byteData = fis.read()) != -1) {
fos.write(byteData);
}
System.out.println("File copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Subscribe to my newsletter
Read articles from Aditi Dede directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
