Java BufferedOutputStream .write (byte[] b, int off, int len)方法

2019-10-16 22:18:25

Java BufferedOutputStream .write (byte[] b, int off, int len)方法

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

public void write(byte[] b,  int off,  int len)  throws IOException

範例

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

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);

    byte[] bytes = { 1, 2, 3, 4, 5 };

    bos.write(bytes, 0, 5);

    bos.flush();

    for (byte b : baos.toByteArray()) {
      System.out.print(b);
    }

  }
}

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

12345