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); } } } |
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); } } } |
Is stream ready: true Stream Closed.
Reference: https://docs.oracle.com/javase/9/docs/api/java/io/PushbackReader.html#close–
Recommended Posts:
- PushbackReader reset() method in Java with Examples
- PushbackReader read() method in Java with Examples
- PushbackReader mark(int) method in Java with Examples
- PushbackReader ready() method in Java with Examples
- PushbackReader markSupported() method in Java with Examples
- PushbackReader skip(long) method in Java with Examples
- PushbackReader read(char, int, int) method in Java with Examples
- StringReader close() method in Java with Examples
- CharArrayReader close() method in Java with Examples
- Formatter close() method in Java with Examples
- Scanner close() method in Java with Examples
- PrintStream close() method in Java with Examples
- Reader close() method in Java with Examples
- ObjectInputStream close() method in Java with examples
- Writer close() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



