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

File Encoding

  1. Unicode Basics: Character Set, Encoding, UTF-8, Codepoint
  2. HTML: Character Sets and Encoding
  3. Unicode in Function Names and Operator Symbol
  4. Python: Unicode Tutorial 🐍
  5. Python: Convert File Encoding
  6. Python: Convert File Encoding for All Files in a Dir
  7. Perl: Unicode Tutorial 🐪
  8. Perl: Convert File Encoding
  9. Ruby: Unicode Tutorial 💎
  10. Java: Convert File Encoding
  11. Linux: Convert File Encoding with iconv

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