Open In App

PushbackInputStream close() method in Java with Examples

Last Updated : 05 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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()



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads