write String to DataOutputStream - Java java.io

Java examples for java.io:OutputStream

Description

write String to DataOutputStream

Demo Code


//package com.java2s;

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

public class Main {
    public static void writeString(DataOutputStream out, String string)
            throws IOException {

        if (string == null)
            string = "";

        // Convert the string to a byte array.
        byte[] bytes = string.getBytes("UTF-8");

        // Write the number of bytes.
        out.writeShort(bytes.length);//from   w w  w.  j  av  a2  s  . c  om

        // Write the array.
        out.write(bytes);
    }
}

Related Tutorials