Share this page 

Convert OEM (DOS) file to Ansi (Windows)Tag(s): IO Internationalization


We are using an InputStreamReader which convert the specified input encoding to Unicode and an OutputStreamWriter which from Unicode to the specified output encoding.

This can be useful when migrating data from a legacy database (ex. Clipper, dBase) to newer DBMS (ex. mySQL, Sybase).

import java.io.*;

public class OemToAnsi {

  public static void main(String args[]) throws Exception{
        if (args.length != 2) {
            System.out.println(
               "Usage : java OemToAnsi inputdosfile outputansifile"
               );
            System.out.println(
               "   note :  codepage input Cp850  codepage output Cp1252"
               );
            System.exit(1);
        }
        // input
        FileInputStream fis =  new FileInputStream(args[0]);
        BufferedReader r =
                new BufferedReader(new InputStreamReader(fis, "Cp850"));
        // output
        FileOutputStream fos = new FileOutputStream(args[1]);
        Writer w =
               new BufferedWriter(new OutputStreamWriter(fos, "Cp1252"));
        String oemString = "";
        while ( (oemString= r.readLine()) != null) {
          w.write(oemString);
          w.flush();
        }
        w.close();
        r.close();
        System.exit(0);
  }
}
See also this related HowTo