Java PushbackInputStream close() Example
PushbackInputStream class close() method example. This example shows you how to use close() method.
Syntax is : public void close() throws IOException
This method closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), unread(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.
Here is the code.
/**
* @(#) ClosePushbackInputStream.java
* A class representing use of method close() of PushbackInputStream
class in java.io Package.
* @Version 10-June-2008
* @author Rose India Team
*/
import java.io.*;
public class ClosePushbackInputStream {
public static void main(String[] args) throws IOException {
try {
byte[] byteArray = new byte[]{'M', 'A', 'H','E','N', 'D', 'R', 'A'};
File file = new File("Mahendra.txt");
// Create object of FileOutputStream class for specified file.
FileOutputStream fOutStream = new FileOutputStream(file);
fOutStream.write(byteArray);
// Create object of PushbackInputStream class for specified stream.
InputStream inStream = new FileInputStream(file);
PushbackInputStream pushInStream =new PushbackInputStream(inStream);
// read content of the file.
System.out.print("Content of the file that can read : ");
for (int c = pushInStream.read(); c != -1; c = pushInStream.read()){
System.out.print((char) c);
}
// close() method call.
pushInStream.close();
System.out.print("\nclose method is called for this input stream.");
pushInStream.read();
} catch (Exception ex) {
System.out.println("\nCan't read more elements bacause file stream"
+ " is closed now.");
}
}
} |
output of the program.
Content of the file that can read : MAHENDRA
close method is called for this input stream.
Can't read more elements bacause file stream is closed now. |
|