Java FilterInputStream close() Example
FilterInputStream class close() method example. This example shows you how to use available() method.
Syntax is : public void close() throws IOException
Method closes this input stream and releases any system resources associated with the stream. This method simply performs in.close().
Here is the code.
/**
* @(#) CloseFilterInputStream.java
* A class representing use of method close() of FilterInputStream
class in java.io Package.
* @Version 09-June-2008
* @author Rose India Team
*/
import java.io.*;
public class CloseFilterInputStream extends FilterInputStream {
CloseFilterInputStream(FileInputStream objFin){
super(objFin);
}
public static void main(String[] args) throws IOException{
try {
byte[] byteArray = new byte[]{'M','A','H','E','N','D','R','A'};
//Create a file with a specified name and file extension.
File file = new File("mahendra.txt");
// Create a output stream and write specified bytes in file.
FileOutputStream objFileStream = new FileOutputStream(file);
objFileStream.write(byteArray);
// create a file input stream to read content of the file.
FileInputStream objFin = new FileInputStream(file);
CloseFilterInputStream obj =new CloseFilterInputStream(objFin);
System.out.print("\nElements read : ");
for (int i = 0; i < 5; i++) {
System.out.print((char) obj.read());
}
// close() method call.
obj.close();
System.out.print("\nclose method is called for this input stream.");
System.out.println((char) obj.read());
}
catch (Exception ex){
System.out.println("\nCan't read more elements bacause file stream"
+" is closed now.");
}
}
} |
Output of the program.
Elements read : MAHEN
close method is called for this input stream.
Can't read more elements bacause file stream is closed now. |
|