Java tutorial
//package com.java2s; import java.io.DataOutputStream; import java.io.IOException; public class Main { public static final String CHARSET = "UTF-8"; public static void writeUTF(DataOutputStream os, String str) throws IOException { byte[] data = str.getBytes(CHARSET); writeShort(os, (short) data.length); os.write(data); } public static void writeShort(DataOutputStream os, short s) throws IOException { os.writeShort(Short.reverseBytes(s)); } }