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








Syntax

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

public void mark(int readAheadLimit)  throws IOException

Example

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

// w w w  . j av a  2s. co  m
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class Main {
  public static void main(String[] args) throws IOException {
    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr);

    // reads and prints data from reader
    System.out.println((char) lnr.read());
    System.out.println((char) lnr.read());

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

    // if this reader supports mark() an reset()
    if (lnr.markSupported()) {
      // reset() repositioned the reader to the mark
      lnr.reset();
      System.out.println("reset() invoked");
      System.out.println((char) lnr.read());
      System.out.println((char) lnr.read());
    }
        lnr.close();
  }
}