Microsoft SDK for Java

Conversion between Local Character Set Encoding and Unicode

Although the Java language includes support for Unicode strings, many text editors use local character set encoding, and many text files contain local character set strings, not Unicode strings. This causes two different problems:

The Microsoft Compiler for Java (jvc) translates local character set encoded string literals in Java source code to Unicode when it compiles the source into class files. When the Microsoft VM runs these class files, the string literals will appear correctly. The java.lang.Runtime methods getLocalizedInputStream and getLocalizedOutputStream will convert between local character set encoding and Unicode.

Note   You should only use the getLocalizedInputStream method on input streams that are known to be encoded in a local character set. Do not use this method on input streams that contain Unicode characters.

Converting Local Encoding to Unicode

The getLocalizedInputStream method converts local character set encoding to Unicode. To use it, you must first call Runtime.getRuntime to get a Runtime object. Then, call the Runtime.getLocalizedInputStream method, passing the input stream object that you want to have converted. The getLocalizedInputStream method returns a DataInputStream object that performs local-character-set-to-Unicode conversion on the input stream. The following example demonstrates this technique.

Example

Runtime rt = Runtime.getRuntime();
FileInputStream fisLoc;  // local
DataInputStream disUni;  // Unicode
String sInput;

fisLoc = new FileInputStream("LOCAL.TXT");
disUni = (DataInputStream)rt.getLocalizedInputStream(fisLoc);
sInput = disUni.readLine();

Converting Unicode to Local Encoding

The getLocalizedOutputStream method converts Unicode to local character set encoding. To use it, you must first call Runtime.getRuntime to get a Runtime object. Then, call the Runtime.getLocalizedOutputStream method, passing the output stream object that you want to have converted. The getLocalizedOutputStream method returns a DataOutputStream object that performs Unicode-to-local-character-set conversion on the output stream. The following example demonstrates this technique.

Example

Runtime rt = Runtime.getRuntime();
FileOutputStream fosUni;  // Unicode
DataOutputStream dosLoc;  // local
String sOutput = "This is a Unicode string";

fosUni = new FileOutputStream("OUTPUT.TXT");
dosLoc = (DataOutputStream)rt.getLocalizedOutputStream(fosUni);
dosLoc.writeChars(sOutput);

© 1999 Microsoft Corporation. All rights reserved. Terms of use.