Java: Convert File Encoding

By Xah Lee. Date:

How 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();
     }
}