Java DataOutputStream .writeBytes (String s)

Syntax

DataOutputStream.writeBytes(String s) has the following syntax.

public final void writeBytes(String s)   throws IOException

Example

In the following code shows how to use DataOutputStream.writeBytes(String s) method.


/*from   w w w  . j a  va  2s.  c  o  m*/

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

public class Main {
  public static void main(String[] args) throws IOException {
    String s = "Hello from java2s.com!";

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

    dos.writeBytes(s);
    dos.flush();

    System.out.println(s + " in bytes:");
    for (byte b : baos.toByteArray()) {
      System.out.print(b + ",");
    }
  }
}

The code above generates the following result.