DBCS, Unicode, and Java Previous
Previous
Introduction
Introduction
Next
Next

Microsoft VM for Java has Working Conversion Methods

Using the getLocalizedInputStream Method , Using the getLocalizedOutputStream Method

Microsoft VM for Java contains working versions of the Runtime methods getLocalizedInputStream and getLocalizedOutputStream. These methods are part of the Java language specification. Using these methods, you can write Java programs that can convert DBCS to Unicode and can convert Unicode to DBCS.


Using the getLocalizedInputStream Method

The getLocalizedInputStream method converts DBCS to Unicode. To use it, you must first call Runtime.getRuntime() to get a run-time object. Then, call the run-time getLocalizedInputStream method, passing the input stream object you want to have converted. getLocalizedInputStream returns a DataInputStream object that does DBCS to Unicode conversion on the input stream. The following example demonstrates this:

Runtime rt = Runtime.getRuntime();
FileInputStream fisDBCS;
DataInputStream disUnicode;
String sInput;

fisDBCS = new FileInputStream("DBCS.TXT");
disUnicode = (DataInputStream)rt.getLocalizedInputStream(fisDBCS);
sInput = disUnicode.readLine(); 

The previous code excerpt shows that DBCS.TXT is a file containing DBCS text strings. fisDBCS is a file input stream object that opens the DBCS.TXT file. disUnicode is a DBCS-to-Unicode input stream that is created by passing fisDBCS through getLocalizedInputStream. The call to disUnicode.readLine() gets a line of DBCS text from DBCS.TXT, converts it to Unicode, and then places the resulting Unicode string in the sInput String object.


Using the getLocalizedOutputStream Method

The getLocalizedOutputStream method converts Unicode to DBCS. To use it, you must first call Runtime.getRuntime() to get a run-time object. Then, call the run-time getLocalizedOutputStream method, passing the output stream object that you want to have converted. getLocalizedOutputStream returns a DataOutputStream object that does Unicode to DBCS conversion on the output stream. Consider the following example:

Runtime rt = Runtime.getRuntime();
FileOutputStream fosUnicode;
DataOutputStream dosDBCS;
String sOutput = "This is a Unicode string";

fosUnicode = new FileOutputStream("OUTPUT.TXT");
dosDBCS = (DataOutputStream)rt.getLocalizedOutputStream(fosUnicode);
dosDBCS.writeChars(sOutput); 

The previous code excerpt, Output.txt, is a new text file that is going to contain DBCS text strings. fosUnicode is a file output stream object that opens the Output.txt file. dosDBCS is a Unicode-to-DBCS output stream that is created by passing fosUnicode through getLocalizedOutputStream. The call to dosDBCS.writeChars() converts the Unicode string sOutput to DBCS, and places the resulting DBCS string in the Output.txt file.

Top© 1997 Microsoft Corporation. All rights reserved. Terms of Use.