Open In App

PushbackReader close() method in Java with Examples

The close() method of PushbackReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results:

Syntax:



public void close()

Parameters: This method does not accepts any parameters

Return Value: This method do not returns any value.



Exception: This method throws IOException if some error occurs while input-output.

Below methods illustrates the working of close() method:

Program 1:




// Java program to demonstrate
// PushbackReader close() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String s = "GeeksForGeeks";
  
            // Initializing a StringReader and PushbackReader
            StringReader stringReader
                = new StringReader(s);
            PushbackReader pushbackReader
                = new PushbackReader(stringReader);
  
            System.out.println("Is stream ready: "
                               + pushbackReader.ready());
  
            // Close the stream using close()
            pushbackReader.close();
            System.out.println("Stream Closed.");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:
Is stream ready: true
Stream Closed.

Program 2:




// Java program to demonstrate
// PushbackReader close() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            // Initializing a StringReader and PushbackReader
            String s = "GFG";
  
            StringReader stringReader
                = new StringReader(s);
            PushbackReader pushbackReader
                = new PushbackReader(stringReader);
  
            System.out.println("Is stream ready: "
                               + pushbackReader.ready());
  
            // Close the stream using close()
            pushbackReader.close();
            System.out.println("Stream Closed.");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:
Is stream ready: true
Stream Closed.

Reference: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#close–


Article Tags :