Friday, February 6, 2026
HomeLanguagesJavaByteArrayInputStream reset() method in Java with Examples

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: 

Java




// 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 stream to marked positions.
        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




// 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 stream to marked positions.
        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()
 

RELATED ARTICLES

Most Popular

Dominic
32489 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12073 POSTS0 COMMENTS
Shaida Kate Naidoo
6995 POSTS0 COMMENTS
Ted Musemwa
7236 POSTS0 COMMENTS
Thapelo Manthata
6946 POSTS0 COMMENTS
Umr Jansen
6930 POSTS0 COMMENTS