Thursday, July 4, 2024
HomeLanguagesJavaPushbackReader close() method in Java with Examples

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:

  • 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–

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments