Skip to content
Related Articles

Related Articles

Improve Article
PushbackInputStream close() method in Java with Examples
  • Last Updated : 05 Jun, 2020

The close() method of PushbackInputStream class in Java is used to closes the input stream and it releases resources of system that are associated with the stream. After calling this method, further calling other method if this class will throw the IOException.

Syntax:

public void close()
           throws IOException

Specified By: This method is specified by the close() method AutoCloseable interface and the close() method of Closeable interface.

Overrides: This method overrides the close() method of FilterInputStream class.

Parameters: This method does not accept any parameter.



Return value: This method does not return any value.

Exceptions: This method throws IOException if an I/O error occurs.

Below programs illustrate close() method of PushbackInputStream class in IO package:

Program 1:




// Java program to illustrate
// PushbackInputStream close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
        try {
  
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E',
                               'K', 'S' };
  
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
  
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr);
  
            for (int i = 0; i < byteArray.length; i++) {
                // Read the character
                System.out.print(
                    (char)pushbackInputStr.read());
            }
  
            // Revoke close()
            pushbackInputStr.close();
        }
        catch (Exception e) {
            System.out.println("Stream is closed");
        }
    }
}
Output:
GEEKS

Program 2:




// Java program to illustrate
// PushbackInputStream close() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
        try {
  
            // Create an array
            byte[] byteArray
                = new byte[] { 'G', 'E', 'E',
                               'K', 'S' };
  
            // Create inputStream
            InputStream inputStr
                = new ByteArrayInputStream(byteArray);
  
            // Create object of
            // PushbackInputStream
            PushbackInputStream pushbackInputStr
                = new PushbackInputStream(inputStr);
  
            // Revoke close()
            pushbackInputStr.close();
  
            for (int i = 0; i < byteArray.length; i++) {
                // Read the character
                System.out.print(
                    (char)pushbackInputStr.read());
            }
        }
        catch (Exception e) {
            System.out.println("Stream is closed");
        }
    }
}
Output:
Stream is closed

References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#close()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :