Write "real" ascii fileTag(s): IO


Java uses Unicode character encoding internally. To pass information to outside world, it may be necessary to use different encoding.

For example, DOS application may use MS ISO-Latin 1(or Codepage 850) to represent french characters like é or à.

Before writting to a file or in a database record it is necessary to change the default String encoding. This done via the InputStreamReader class for input and OutputStreamWriter for output.

InputStreamReader converts from the specified input encoding to Unicode while the OutputStreamWriter converts from Unicode to the specified output encoding.

import java.io.*;
import java.awt.*;

public class DosString {
  public static void main(String args[]){
    String javaString = "é &agrace; \u00e9";  // Unicode for "é"

    try {
      // output : Unicode to Cp850 (MS-DOS Latin-1)
      FileOutputStream fos = new FileOutputStream("out.dat");
      Writer w = 
        new BufferedWriter(new OutputStreamWriter(fos, "Cp850"));
      w.write(JavaString);
      w.flush();
      w.close();  

      // input`: Cp850  to Unicode
      FileInputStream fis =  new FileInputStream("out.dat");
      BufferedReader r = 
        new BufferedReader(new InputStreamReader(fis, "Cp850"));
      String dosString = r.readLine();
      r.close();
      Frame f = new Frame();
      f.setSize(100,100);
      f.add(new Label(dosString));
      f.setVisible(true);
      }
   catch (Exception e) {
      e.printStackTrace();
      }
   }
}
NOTE: When the character encoding is not specified, the default encoding is used. You can find out the current default encoding by looking at file.encoding property with System.getProperty("file.encoding");.
blog comments powered by Disqus