Java BufferedInputStream.read (byte[] b, int off, int len)方法

2019-10-16 22:18:36

Java BufferedInputStream .read (byte[] b, int off, int len)方法

BufferedInputStreamBufferedInputStream.read (byte[] b, int off, int len)方法具有以下語法。

public int read()  throws IOException

範例

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

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read number of bytes available
    int numByte = bis.available();

    // byte array declared
    byte[] buf = new byte[numByte];

    // read byte into buf , starts at offset 2, 3 bytes to read
    bis.read(buf, 2, 3);

    // for each byte in buf
    for (byte b : buf) {
      System.out.println((char) b + ": " + b);
    }
  }
}