Java DataOutputStream.writeBoolean (boolean v)型別

2019-10-16 22:17:23

DataOutputStream.writeBoolean(boolean v)方法範例

DataOutputStreamDataOutputStream.writeBoolean(boolean v)方法具有以下語法。

public final void writeBoolean(boolean v)   throws IOException

範例

在下面的程式碼中展示了如何使用DataOutputStream.writeBoolean(boolean v)方法。

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

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

    boolean[] bools = { true, false, false, true, true, true };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    for (boolean bool : bools) {
      dos.writeBoolean(bool);
    }

    dos.flush();//::w w  W .YIi b A I  . COm 

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

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

100111