Tuesday, November 19, 2024
Google search engine
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–

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments