Thursday, August 28, 2025
HomeLanguagesJavaPushbackReader reset() method in Java with Examples

PushbackReader reset() method in Java with Examples

The reset() method of PushbackReader Class in Java is used to reset the Stream. In the case of PushbackReader, this method always throws an exception as this method isn’t supported by the PushbackReader.

Syntax:

public void reset()

Parameters: This method do not accepts any parameter.

Return Value: This method do not returns any value.

Exception: This method throws IOException always as the reset() method is not supported.

Below methods illustrates the working of reset() method:

Program 1:




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


Output:

java.io.IOException: mark/reset not supported

Program 2:




// Java program to demonstrate
// PushbackReader reset() 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);
  
            // reset the stream position
            pushbackReader.reset();
  
            // Close the stream
            pushbackReader.close();
            System.out.println("Stream Closed.");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.io.IOException: mark/reset not supported

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

RELATED ARTICLES

Most Popular

Dominic
32236 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6609 POSTS0 COMMENTS
Nicole Veronica
11779 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11828 POSTS0 COMMENTS
Shaida Kate Naidoo
6719 POSTS0 COMMENTS
Ted Musemwa
7002 POSTS0 COMMENTS
Thapelo Manthata
6678 POSTS0 COMMENTS
Umr Jansen
6690 POSTS0 COMMENTS