Sometimes it's useful to be able to transcode between different character encodings in Java. For example, Microsoft uses UTF-16 to produce a text file export from msinfo32.exe. For many people, this just means the file is twice as large as it needs to be, since often the characters are only 'Latin1' anyway. You can use the code below to deal with this. The source code is HERE and the dependent source (IOUtils) is HERE. A sample invocation of the transcoding scenario mentioned above might be

java XCode -i sysinfo16.txt -ie UTF-16 -o sysinfo8.txt -oe UTF-8

import net.proteanit.io.IOUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import java.util.HashMap;
import java.util.Map;


public class XCode {
    /**
     * Transcode between character encodings using Java
     *
     @param args Command line arguments - see usage
     */
    public static void main(String[] args) {
        try {
            if ((args.length == 1) && args[0].matches("-?-h(?:elp)?")) {
                XCode.usage();
            } else {
                new XCode().process(args);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void usage() {
        System.err.println(
            "Usage: java XCode [-i (input file, '-' for stdin)] [-ie (input encoding)] [-o (output file)] [-oe (output encoding, '-' for stdout)]");
    }

    private void process(String[] args)
        throws IOException, UnsupportedEncodingException {
        // Get argument from command line and map them
        Map<String, String> argMap = new HashMap<String, String>();

        for (int i = 0; i < args.length; i += 2) {
            argMap.put(args[i].substring(1), args[i + 1]);
        }

        final String STD = "-";
        String DEFAULT_ENCODING = System.getProperty("file.encoding");
        String inFile = argMap.get("i");

        if (inFile == null) {
            inFile = STD;
        }

        String outFile = argMap.get("o");

        if (outFile == null) {
            outFile = STD;
        }

        String inEncoding = argMap.get("ie");

        if (inEncoding == null) {
            inEncoding = DEFAULT_ENCODING;
        }

        String outEncoding = argMap.get("oe");

        if (outEncoding == null) {
            outEncoding = DEFAULT_ENCODING;
        }

        transcode(inFile, inEncoding, outFile, outEncoding);
    }

    public void transcode(String inFile, String inEnc, String outFile,
        String outEnc) throws IOException, UnsupportedEncodingException {
        Reader in = null;

        if ("-".equals(inFile)) {
            in = new InputStreamReader(System.in, inEnc);
        } else {
            in = new InputStreamReader(new FileInputStream(inFile), inEnc);
        }

        Writer out = null;

        if ("-".equals(outFile)) {
            out = new OutputStreamWriter(System.out, outEnc);
        } else {
            out = new OutputStreamWriter(new FileOutputStream(outFile), outEnc);
        }

        IOUtils.copyReader(in, out);
    }
}