Java FilterReader.reset()方法範例

2019-10-16 22:13:39

FilterReader.reset()方法範例

FilterReader.reset()具有以下語法。

public void reset()  throws IOException

範例

在下面的程式碼中展示了如何使用FilterReader.reset()方法。

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class Main {// @  w  w w . y i  iBa i . c O M 
  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());

  }
}

上面的程式碼生成以下結果。

Ready to read? true