
Java Programming Examples
|
589
Unicode to non-Unicode conversion—export
Here is the Java code for converting a Unicode-encoded text stream into Shi-JIS encod-
ing. Notice that this function does not return any information to the calling statement—it
merely reads in a text stream and outputs to another text stream.
File o = new File("output.sjs");
FileOutputStream tmpout = new FileOutputStream(o);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(tmpout,"Shift_JIS"));
Aer the BueredWriter object out is established, Unicode data that is subsequently out-
put is automatically converted to Shi-JIS encoding.
out.println("\u6CB3\u8C5A");
out.close();
e two Unicode characters 63 () and 85 () that are fed to the println()
method become Shi-JIS <89 > and <93 8> in the output le called output.sjs. Creat-
ing a UTF-8–encoded output le is accomplished in the same way, but the UTF-8 charset
designator should be used instead of Shi_JIS.
It is also possible to output directly in Unicode, demonstrated as follows:
PrintWriter out = new PrintWriter (
new BufferedWriter (
new OutputStreamWriter (
new FileOutputStream("output.ucs"), "UTF-16BE"
)
)
);
We can then output the same Unicode characters, without any code conversion applied,
as follows:
out.println("\u6CB3\u8C5A");
out.close();
is time, the output is exactly 63 () and 85 ().
Java Charset Designators
In order