Java DataOutputStream .writeBoolean (boolean v)

Syntax

DataOutputStream.writeBoolean(boolean v) has the following syntax.

public final void writeBoolean(boolean v)   throws IOException

Example

In the following code shows how to use DataOutputStream.writeBoolean(boolean v) method.


//  w  w w .ja  v a  2 s.  com
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();

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

The code above generates the following result.