import java.io.*; /***************************************************************************** SaveUTF16 -- Code for Saving UTF-16 or UTF-8 to Disk and Reading it 03 B1 05 D0 Unicode 0000 0011 1011 0001 0000 0101 1101 0000 CE B1 D7 90 UTF-8 1100 1110 1011 0001 1101 0111 1001 0000 ***0 1110 **11 0001 ***1 0111 **01 0000 * UTF-8 Rec id's 03 B1 05 D0 *****************************************************************************/ class SaveUTF16 { public static void main (String[] args) { int i; String s; char[] ac; byte[] ab; FileInputStream fis; FileOutputStream fos; InputStreamReader isr; OutputStreamWriter osw; ac = new char[2]; ac[0] = '\u03b1'; ac[1] = '\u05d0'; try { fos = new FileOutputStream ("test.dat"); osw = new OutputStreamWriter (fos, "UTF-16"); // or UTF-8 cf. docs/api/java/lang/package-summary.html#charenc osw.write ('a'); osw.write ('b'); osw.write ('c'); osw.write (ac); osw.write ('d'); osw.write ('e'); osw.write ('f'); osw.write (ac); osw.close (); fis = new FileInputStream ("test.dat"); isr = new InputStreamReader (fis, "UTF-16"); ac = new char [10]; isr.read (ac); for (i = 0; i < ac.length; i++) { System.out.println ("ch->" + ac[i] + "<- 0x" + Integer.toHexString (ac[i])); } s = "\u03b1\u05d0"; ab = s.getBytes ("UTF-16"); // another useful service System.out.println ("1st 2 bytes are sign of UTF-16"); for (i = 0; i < ab.length; i++) { System.out.println ("b 0x" + Integer.toHexString (ab[i])); } s = new String (ab, "UTF-16"); // another userful service } catch (Exception e) { System.out.println ("Exception: " + e); } } }