ByteArrayInputStream reset() method in Java with Examples
Last Updated :
26 Jul, 2021
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:
Java
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception
{
byte [] buf = { 5 , 6 , 7 , 8 , 9 };
ByteArrayInputStream exam
= new ByteArrayInputStream(buf);
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println(exam.read());
exam.reset();
System.out.println( "\nreset() invoked" );
System.out.println(exam.read());
System.out.println(exam.read());
}
}
|
Output:
5
6
7
reset() invoked
5
6
Program 2:
Java
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception
{
byte [] buf = { 1 , 2 , 3 , 4 };
ByteArrayInputStream exam
= new ByteArrayInputStream(buf);
System.out.println(exam.read());
System.out.println(exam.read());
System.out.println(exam.read());
exam.mark( 1 );
exam.reset();
System.out.println( "\nreset() invoked" );
System.out.println(exam.read());
System.out.println(exam.read());
}
}
|
Output:
1
2
3
reset() invoked
4
-1
Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#reset()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...