Java ByteArrayOutputStream.write (int b)方法範例

2019-10-16 22:16:59

Java ByteArrayOutputStream.write (int b)方法範例

ByteArrayOutputStreamwrite (int b)方法的語法如下。

public void write (int b)

範例

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // write byte array to the output stream
    baos.write(55);// => wW w .y Ii B AI . C om

    // converts the byte to the default charset value
    String str = baos.toString();

    // prints the string
    System.out.println(str);

  }
}

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

7