
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - PushbackInputStream close() method
Description
The Java PushbackInputStream close() 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.
The close() method
Closes the stream and releases any system resources associated with it.
After closing, further read(), unread(), or available() calls will throw an IOException.
This also closes the underlying input stream.
Declaration
Following is the declaration for java.io.PushbackInputStream.close() method.
public void close()
Parameters
NA
Return Value
This method does not return a value.
Exception
IOException − If an I/O error occurs.
Example - Usage of PushbackInputStream close() method
The following example shows the usage of PushbackInputStream close() method.
PushbackInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.PushbackInputStream; public class PushbackInputStreamDemo { public static void main(String[] args) { // declare a buffer and initialize its size: byte[] arrByte = new byte[1024]; // create an array for our message byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o'}; // create object of PushbackInputStream class for specified stream InputStream is = new ByteArrayInputStream(byteArray); PushbackInputStream pis = new PushbackInputStream(is); try { // read from the buffer one character at a time for (int i = 0; i < byteArray.length; i++) { // read a char into our array arrByte[i] = (byte) pis.read(); // display the read byte System.out.print((char) arrByte[i]); } System.out.println("\nClosing the stream..."); // close the stream pis.close(); System.out.println("Stream closed."); } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Hello Closing the stream... Stream closed.
Example - Close a stream after reading
The following example shows the usage of PushbackInputStream close() method.
PushbackInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.PushbackInputStream; import java.io.IOException; public class PushbackInputStreamDemo { public static void main(String[] args) { byte[] data = "Java".getBytes(); try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data))) { int ch; while ((ch = pbis.read()) != -1) { System.out.print((char) ch); // Output: Java } // Stream will be closed automatically by try-with-resources } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Java
Explanation
A PushbackInputStream is created over a ByteArrayInputStream.
It reads and prints all characters.
The stream is closed automatically using try-with-resources.
Example - Manually closing a stream and attempting to read
The following example shows the usage of PushbackInputStream close() method.
PushbackInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.PushbackInputStream; import java.io.IOException; public class PushbackInputStreamDemo { public static void main(String[] args) { byte[] data = "Stream".getBytes(); PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data)); try { int ch = pbis.read(); // Read 'S' System.out.println("Read: " + (char) ch); // Output: S pbis.close(); // Manually close the stream pbis.read(); // This will throw IOException } catch (IOException e) { System.out.println("Exception after close: " + e); // Expected IOException } } }
Output
Let us compile and run the above program, this will produce the following result−
Read: S Exception after close: java.io.IOException: Stream closed
Explanation
We read the first character 'S'.
After calling close(), any further operation (like read()) throws an IOException, demonstrating that the stream is no longer usable.