Java IO Tutorial - Java InputStream.mark(int readlimit)








Syntax

InputStream.mark(int readlimit) has the following syntax.

public void mark(int readlimit)

Example

In the following code shows how to use InputStream.mark(int readlimit) method.

// w ww  . ja va 2s  . 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");

    // read and print characters one by one
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    // mark is set on the input stream
    is.mark(0);

    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    if (is.markSupported()) {
      // reset invoked if mark() is supported
      is.reset();
      System.out.println("Char : " + (char) is.read());
      System.out.println("Char : " + (char) is.read());
    }
    is.close();
  }
}