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

2019-10-16 22:16:58

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

ByteArrayOutputStreamwrite(byte[] b, int off, int len)方法的語法如下。

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

範例

在下面的程式碼顯示如何使用ByteArrayOutputStream.write(byte[] b, int off, int len)建構函式。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
// from:w ww.y i I bai .cO m
public class Main {
  public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // write byte array to the output stream
    baos.write(bs, 3, 2);

    // read all the bytes in the output stream
    for (byte b : baos.toByteArray()) {
      System.out.println(b);
    }

  }
}

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

68
69