ByteArrayInputStream.markSupported()方法範例

2019-10-16 22:17:10

Java ByteArrayInputStream.markSupported()方法範例

ByteArrayInputStreamJava ByteArrayInputStream.markSupported()方法的語法如下。

public boolean markSupported()

範例

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

import java.io.ByteArrayInputStream;
import java.io.IOException;

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

    byte[] buf = { 65, 66, 67, 68, 69 };

    // create new byte array input stream
    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    // test support for mark() and reset() methods invocation
    boolean isMarkSupported = bais.markSupported();
    System.out.println("Is mark supported : " + isMarkSupported);
    System.out.println("Following is the proof:");

    // print bytes // At: wW W .Y iI b AI  .C OM
    System.out.println(bais.read());
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Mark() invocation");

    // mark() invocation;
    bais.mark(0);
    System.out.println(bais.read());
    System.out.println(bais.read());

    System.out.println("Reset() invocation");

    // reset() invocation
    bais.reset();
    System.out.println(bais.read());
    System.out.println(bais.read());

  }
}

執行上面的程式碼,得到如下結果 -

Is mark supported : true
Following is the proof:
65
66
67
Mark() invocation
68
69
Reset() invocation
68
69