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/test/gb18030.txt"); File outfile = new File("/Users/t/test/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”.
For the same task done with Python, Perl, Emacs, see: Python & Perl: Converting a File's Encoding.