Java DataOutputStream .writeByte (int v)

Syntax

DataOutputStream.writeByte(int v) has the following syntax.

public final void writeByte(int v)   throws IOException

Example

In the following code shows how to use DataOutputStream.writeByte(int v) method.


/*  w  w w .  j  a v a  2  s  .  c om*/

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

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

    byte[] buf = { 12, 15, 18, 20, 22, 24 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream dos = new DataOutputStream(baos);

    for (byte b : buf) {
      dos.writeByte(b);
    }
    dos.flush();

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

  }
}

The code above generates the following result.