Last Updated : 04 Jan, 2021
A Closeable is a source or destination of the data that needs to be closed. The close() method is invoked when we need to release resources that are being held by objects such as open files. It is one of the important interfaces to stream classes. Closeable Interface was introduced in JDK 5 and is defined in java.io. From JDK 7+, we are supposed to use AutoCloseable interface. The closeable interface is an older interface that was introduced to preserve backward compatibility.
The Hierarchy of Closeable interface
The Closeable interface extends AutoCloseable interface therefore any class that implements Closeable also implements AutoCloseable.
Declaration
public interface Closeable extends AutoCloseable { public void close() throws IOException; }
Implementing the Closeable interface
import java.io.Closeable; import java.io.IOException; public class MyCustomCloseableClass implements Closeable { @Override public void close() throws IOException { // close resource System.out.println("Closing"); } }
close() method of Closeable interface
The close() method is invoked to release resources that the object is holding. If the stream is already closed then invoking the close method does not have any effects.
Syntax
public void close() throws IOException
Note: Closeable is idempotent, which means calling the close() method more than once has no side effects.
Limitations of Closeable Interface
Closeable throws only IOException and it cannot be changed without breaking legacy code. Therefore, AutoCloseable was introduced as it can throw an Exception.
SuperInterface of Closeable:
SubInterfaces of Closeable:
Implementing Classes:
Closeable vs AutoCloseable
try(FileInputStream fin = new FileInputStream(input)) { // Some code here }
Usage of try-with Block with Closeable
Since Closeable inherits the properties of AutoCloseable interface, therefore the class implementing Closeable can also use try-with-resources block. Multiple resources can be used inside a try-with-resources block and have them all automatically closed. In this case, the resources will be closed in the reverse order in which they were created inside the brackets.
Java
// Java program to illustrate
// Automatic Resource Management
// in Java with multiple resource
import java.io.*;
class Resource {
public static void main(String s[])
{
// note the order of opening the resources
try (Demo d = new Demo(); Demo1 d1 = new Demo1()) {
int x = 10 / 0;
d.show();
d1.show1();
}
catch (ArithmeticException e) {
System.out.println(e);
}
}
}
// custom resource 1
class Demo implements Closeable {
void show() { System.out.println("inside show"); }
public void close()
{
System.out.println("close from demo");
}
}
// custom resource 2
class Demo1 implements Closeable {
void show1() { System.out.println("inside show1"); }
public void close()
{
System.out.println("close from demo1");
}
}
close from demo1 close from demo java.lang.ArithmeticException: / by zeroMethods of Closeable interface
Method
Description
close() Closes this stream and releases any system resources associated with it.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4