PushbackInputStream reset() method in Java with Examples

The reset() method of PushbackInputStream class in Java is used to reset the the steam to the position where mark() method was called. This method does nothing for PushbackInputStream.

Syntax:

public void reset()
           throws IOException

Overrides: This method overrides the reset() method of FilterInputStream class.

Parameters: This method does not accept any parameter.

Return value: This method does not return any value.



Exceptions: This method throws IOException whenever this method is called.

Below programs illustrate reset() method of PushbackInputStream class in IO package:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// PushbackInputStream reset() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create an array
        byte[] byteArray
            = new byte[] { 'G', 'E', 'E',
                           'K', 'S' };
  
        // Create inputStream
        InputStream inputStr
            = new ByteArrayInputStream(byteArray);
  
        // Create object of
        // PushbackInputStream
        PushbackInputStream pushbackInputStr
            = new PushbackInputStream(inputStr);
  
        for (int i = 0; i < byteArray.length; i++) {
            // Read the character
            System.out.print(
                (char)pushbackInputStr.read());
        }
  
        // Revoke reset() but it does nothing
        pushbackInputStr.reset();
    }
}

chevron_right


Output:

Exception in thread “main” java.io.IOException: mark/reset not supported
GEEKS

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// PushbackInputStream reset() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create an array
        byte[] byteArray
            = new byte[] { 'H', 'E', 'L',
                           'L', 'O' };
  
        // Create inputStream
        InputStream inputStr
            = new ByteArrayInputStream(byteArray);
  
        // Create object of
        // PushbackInputStream
        PushbackInputStream pushbackInputStr
            = new PushbackInputStream(inputStr);
  
        // Revoke reset()
        pushbackInputStr.reset();
  
        for (int i = 0; i < byteArray.length; i++) {
            // Read the character
            System.out.print(
                (char)pushbackInputStr.read());
        }
    }
}

chevron_right


Output:

Exception in thread “main” java.io.IOException: mark/reset not supported

References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#reset()

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.