Share this page 

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.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"));

      // input`: Cp850  to Unicode
      FileInputStream fis =  new FileInputStream("out.dat");
      BufferedReader r = 
        new BufferedReader(new InputStreamReader(fis, "Cp850"));
      String dosString = r.readLine();
      Frame f = new Frame();
      f.add(new Label(dosString));
   catch (Exception e) {
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");.