View Priya Saini's profile
The Closeable interface includes only one abstract method, close().
When close() method is called, the system resources held by the stream object
are released and can be used by other part of the program (avoids memory leaks).
The close method is invoked to release resources that the object is holding
Also if the super class implements this interface, the sub class can use this method.
For example, the InputStream implements this method and its subclass FileInputStream can use close() method.
So, all the streams can use close() method as the super classes of all streams,
InputStream, OutputStream, Reader and Writer, implement Closeable interface.
Syntax:
public interface Closeable extends AutoCloseable{
}
Throws:
IOException - if an I/O error occurs
Many destination streams implement this interface and overrides the flush() method.
this method is called to flushed out the data to the destination file to write that held in buffers
Following are a few classes signature that implements Closeable Flushable Java (either one or both)
The close() and flush() methods throw a checked exception IOException and must be handled when these methods are used.
public abstract class InputStream extends Object implements Closeable
public abstract class OutputStream extends Object implements Closeable, Flushable
public abstract class Reader extends Object implements Readable, Closeable
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
public class PrintStream extends FilterOutputStream implements Appendable, Closeable
Closeable and Flushable interface
Closeable and Flushable interfaces were introduced with JDK 1.5 version and part of java.io package.1. Closeable interface (of Closeable Flushable Java)
The Closeable interface includes only one abstract method, close().
When close() method is called, the system resources held by the stream object
are released and can be used by other part of the program (avoids memory leaks).
The close method is invoked to release resources that the object is holding
Also if the super class implements this interface, the sub class can use this method.
For example, the InputStream implements this method and its subclass FileInputStream can use close() method.
So, all the streams can use close() method as the super classes of all streams,
InputStream, OutputStream, Reader and Writer, implement Closeable interface.
Syntax:
public interface Closeable extends AutoCloseable{
}
Throws:
IOException - if an I/O error occurs
2. Flushable interface (of Closeable Flushable Java)
The Flushable interface includes only one method – flush().Many destination streams implement this interface and overrides the flush() method.
this method is called to flushed out the data to the destination file to write that held in buffers
Following are a few classes signature that implements Closeable Flushable Java (either one or both)
The close() and flush() methods throw a checked exception IOException and must be handled when these methods are used.
Examples:-
public abstract class InputStream extends Object implements Closeablepublic abstract class OutputStream extends Object implements Closeable, Flushable
public abstract class Reader extends Object implements Readable, Closeable
public abstract class Writer extends Object implements Appendable, Closeable, Flushable
public class PrintStream extends FilterOutputStream implements Appendable, Closeable