Skip to content
Related Articles

Related Articles

PushbackReader close() method in Java with Examples
  • Last Updated : 25 Apr, 2019

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:

  • If the stream is open, it closes the stream releasing the resources
  • If the stream is already closed, it will have no effect.
  • If any read or other similar operation is performed on the stream, after closing, it raises IOException

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–

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 :