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

Java

  1. Install
  2. javac java jar javadoc

Java Basics

  1. Print String
  2. Strings
  3. Power Function
  4. Java Types and Type Conversion
  5. Function
  6. Arrays

Class, Inheritance

  1. extends
  2. Constructor
  3. Constructor Return Type
  4. Extending a Class that has Explicit Constructors
  5. static
  6. this
  7. super
  8. Access Specifiers

Abstract class, Interface

  1. abstract
  2. Interface

Package

  1. Package

Misc

  1. Collection, Map
  2. Read/Write File
  3. Convert File Encoding
  4. Complex Numbers in Java
  5. Unicode
  6. Number Literals