Java IO Tutorial - Java LineNumberReader.reset()








Syntax

LineNumberReader.reset() has the following syntax.

public void reset()  throws IOException

Example

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

/*  w w  w .jav  a 2s.c  o m*/
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

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

    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 stream to the mark
      lnr.reset();
      System.out.println("reset() invoked");
      System.out.println((char) lnr.read());
      System.out.println((char) lnr.read());
    }
    lnr.close();
  }
}