Java InputStream.reset()

Syntax

InputStream.reset() has the following syntax.

public void reset()  throws IOException

Example

In the following code shows how to use InputStream.reset() method.


//from   w  ww  .ja v a2  s. co  m
import java.io.FileInputStream;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws Exception {

    InputStream is = new FileInputStream("C://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()");
    }
    is.close();
  }
}