Java - InputStream reset() method



Description

The Java InputStream reset() moves the stream back to the last marked position, allowing the stream to re-read data from an earlier point. It works only if the stream supports marking (markSupported() should return true) and if mark(int readlimit) was called before reset().

Declaration

Following is the declaration for java.io.InputStream.reset() method −

public void reset()

Parameters

NA

Return Value

The method does not return any value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of InputStream reset() method

The following example shows the usage of Java InputStream reset() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;

      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         System.out.println("Characters printed:");
         // create new buffered reader
         
         // reads and prints BufferedReader
         System.out.println((char)is.read());
         System.out.println((char)is.read());
         
         // mark invoked at this position
         is.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)is.read());
         System.out.println((char)is.read());

         // reset() repositioned the stream to the mark
         if(is.markSupported()) {
            is.reset();
            System.out.println("reset() invoked");
            System.out.println((char)is.read());
            System.out.println((char)is.read());
         } else {
            System.out.print("InputStream does not support reset()");
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Characters printed:
A
B
mark() invoked
C
D
InputStream does not support reset()

Example - Using reset() with BufferedInputStream

The following example shows the usage of Java InputStream reset() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) {
      try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + inputStream.markSupported());

         // Read and print the first character
         System.out.print((char) inputStream.read());

         // Mark the current position
         inputStream.mark(5); // Allows reading up to 5 bytes before mark expires

         // Read next two characters
         System.out.print((char) inputStream.read());
         System.out.print((char) inputStream.read());

         // Reset back to the marked position
         inputStream.reset();

         // Read again from the marked position
         System.out.print((char) inputStream.read());
         System.out.print((char) inputStream.read());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Mark supported? true
Helo
elo

Explanation

  • Uses BufferedInputStream, which supports marking.

  • Calls markSupported(), which returns true.

  • Reads one character and then marks the position.

  • Reads two more characters, then resets to the marked position.

  • Reads again from the marked position.

Example - Using reset() with ByteArrayInputStream

The following example shows the usage of Java InputStream reset() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws IOException{
      byte[] data = "Microservices".getBytes();
      InputStream inputStream = new ByteArrayInputStream(data);

      System.out.println("Mark supported? " + inputStream.markSupported());

      inputStream.mark(6); // Mark at the beginning
      System.out.print((char) inputStream.read()); // Read 'M'
      System.out.print((char) inputStream.read()); // Read 'i'

      inputStream.reset(); // Reset to the marked position
      System.out.print((char) inputStream.read()); // Read 'M' again

      try {
         inputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Mark supported? true
MiM

Explanation

  • Uses ByteArrayInputStream, which supports marking.

  • Calls markSupported(), which returns true.

  • Marks at the beginning of the input stream.

  • Reads two characters (M and i).

  • Resets to the marked position and reads M again.

java_io_inputstream.htm
Advertisements