Java IO Tutorial - Java FilterReader.mark(int readAheadLimit)








Syntax

FilterReader.mark(int readAheadLimit) has the following syntax.

public void mark(int readAheadLimit)  throws IOException

Example

In the following code shows how to use FilterReader.mark(int readAheadLimit) method.

//  ww w  .ja  v  a 2s . c  om
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

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

    Reader r = new StringReader("ABCDEF");

    // create new filter reader
    FilterReader fr = new FilterReader(r) {};

    // reads and prints FilterReader
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

    // mark invoked at this position
    fr.mark(0);
    System.out.println("mark() invoked");
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

    // reset() repositioned the stream to the mark
    fr.reset();
    System.out.println("reset() invoked");
    System.out.println((char) fr.read());
    System.out.println((char) fr.read());

  }
}

The code above generates the following result.