Java IO Tutorial - Java FilterReader.reset()








Syntax

FilterReader.reset() has the following syntax.

public void reset()  throws IOException

Example

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

/*from  w ww.  j a va  2s  .co  m*/

import java.io.FilterReader;
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) {
    };

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

    // mark is set on the filter reader
    fr.mark(0);
    System.out.println("Char : " + (char) fr.read());
    System.out.println("reset() invoked");

    // reset is called
    fr.reset();

    // read and print characters
    System.out.println("char : " + (char) fr.read());
    System.out.println("char : " + (char) fr.read());

  }
}

The code above generates the following result.