ByteArrayInputStream reset() method in Java with Examples

The reset() method is a built-in method of the Java.io.ByteArrayInputStream is invoked by mark() method. It repositions the input stream to the marked position.

Syntax:

public void reset()

Parameters: The function does not accepts any parameter.

Return Value: The function does not returns anything.

Below is the implementation of the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        byte[] buf = { 5, 6, 7, 8, 9 };
  
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
  
        // print bytes
        System.out.println(exam.read());
        System.out.println(exam.read());
        System.out.println(exam.read());
  
        // Use of reset() method :
        // repositioning the stram to marked positions.
        exam.reset();
  
        System.out.println("\nreset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}

chevron_right


Output:

5
6
7

reset() invoked
5
6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        byte[] buf = { 1, 2, 3, 4 };
  
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
  
        // print bytes
        System.out.println(exam.read());
        System.out.println(exam.read());
        System.out.println(exam.read());
  
        exam.mark(1);
  
        // Use of reset() method :
        // repositioning the stram to marked positions.
        exam.reset();
  
        System.out.println("\nreset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}

chevron_right


Output:

1
2
3

reset() invoked
4
-1

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.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.