Xah Lee, 2005-03-28
Question: How can i use Java to convert a file encoded in gb18030 to utf-16?
Here's the solution:
import java.io.File; import java.io.Reader; import java.io.Writer; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.FileInputStream; import java.io.FileOutputStream; public class charrs { public static void main(String[] args) throws IOException { File infile = new File("/Users/t/web/java-a-day/x/xx/x-gb18030.txt"); File outfile = new File("/Users/t/web/java-a-day/x/xx/x-utf16.txt"); Reader in = new InputStreamReader(new FileInputStream(infile), "GB18030"); Writer out = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-16"); int c; while ((c = in.read()) != -1){ out.write(c);} in.close(); out.close(); } }
Note that 3 levels of classes are involved: File, FileInputSream, InputStreamReader. And note that the read method returns “int”, not “Character”.
Java's exceedingly complex IO is a aftermath of pure OOP approach.
For the same task done with Python, see: http://xahlee.org/perl-python/charset_encoding.html
See also:
Page created: 2005-03. © 2005 by Xah Lee.