Java DataOutputStream.write(int b)

Syntax

DataOutputStream.write(int b) has the following syntax.

public void write(int b)  throws IOException

Example

In the following code shows how to use DataOutputStream.write(int b) method.


/*from   w  ww .j av  a 2s.  co m*/
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

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

    int[] buf = { 65, 66, 67, 68, 69, 70, 71 };

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

    for (int i : buf) {
      dos.write(i);
    }

    dos.flush();

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

The code above generates the following result.