ByteArrayInputStream.read(byte[] b, int off, int len)方法範例

2019-10-16 22:17:13

Java ByteArrayInputStream.read(byte[] b, int off, int len)方法範例

ByteArrayInputStreamJava ByteArrayInputStream.read(byte[] b, int off, int len)方法的語法如下。

public int read(byte[] b, int off, int len)

範例

在下面的程式碼中展示了如何使用ByteArrayInputStream.read(byte[] b, int off, int len)方法。

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 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    int b = 0;
    while ((b = bais.read()) != -1) {
      char c = (char) b;
      System.out.println("byte :" + b + "; char : " + c);
    }
    System.out.print(bais.read() + " Reached the end");
  }
}

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

Bytes read: 2
0
: Null
0
: Null
65
: A
66
: B