Java CharArrayReader.markSupported()方法範例

2019-10-16 22:15:29

Java CharArrayReader.markSupported()方法範例

CharArrayReaderCharArrayReader.markSupported()方法的語法如下。

public boolean markSupported()

範例

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

import java.io.CharArrayReader;

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

    char[] ch = { 'A', 'B', 'C', 'D', 'E' };


    CharArrayReader car = new CharArrayReader(ch);

    // verifies if the stream support mark() method
    boolean bool = car.markSupported();
    System.out.println("Is mark supported : " + bool);
    System.out.println("Proof:");

    // read and print the characters from the stream
    System.out.println(car.read());
    System.out.println(car.read());

    // mark() is invoked at this position
    car.mark(0);
    System.out.println("Mark() is invoked");
    System.out.println(car.read());
    System.out.println(car.read());

    // reset() is invoked at this position
    car.reset();
    System.out.println("Reset() is invoked");
    System.out.println(car.read());
    System.out.println(car.read());
    System.out.println(car.read());

  }
}

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

Is mark supported : true
Proof:
65
66
Mark() is invoked
67
68
Reset() is invoked
67
68
69