Previous | Next | Trail Map | Internationalization | Converting Non-Unicode Text

Character and Byte Streams

The java.io package provides classes that allow you to convert between Unicode character streams and byte streams of non-Unicode text. With the InputStreamReader(in the API reference documentation)class, you can convert byte streams to character streams. You use the OutputStreamWriter(in the API reference documentation)class to translate character streams into byte streams.

(figure here -- PENDING)

When you create InputStreamReader and OutputStreamWriter objects, you specify the byte encoding that you want to convert. For example, if you want to translate a text file in the UTF8 encoding into Unicode, you would create an InputStreamReader as follows:

FileInputStream fis = new FileInputStream("output.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
If you omit the encoding identifier, InputStreamReader and OutputStreamWriter will rely on the default encoding. Like the list of supported encodings, the default encoding may vary with the Java platform. On version 1.1 of the Java Development Kit, the default encoding is 8859_1 (ISO-Latin-1). This default is set in the file.encoding system property. You can determine which encoding an InputStreamReader or OutputStreamWriter will use by invoking the getEncoding method. In the following example, we invoke this method to determine that the default encoding on our platform is 8859_1:
InputStreamReader defaultReader = new InputStreamReader(fis);
System.out.println(defaultReader.getEncoding());
You specify an InputStream when creating an InputStreamReader, and an OutputStream when constructing an OutputStreamWriter. InputStream and OutputStream are abstract superclasses of all input and output byte streams. This allows you to perform conversions on any of the byte streams that belong to their subclasses. For instance, with an InputStreamReader you can convert non-Unicode text from a FileInputStream or PipedInputStream, because they are both subclasses of InputStream.

In the example that follows, we'll show you how to perform character set conversions with the InputStreamReader and OutputStreamWriter classes. The full source code for this example is in the file called StreamConverter.java. In this example, we convert a sequence of Unicode characters from a String object into a FileOutputStream of bytes encoded in UTF8. The method that performs the conversion is called writeOutput:

static void writeOutput(String str) {

    try {
       FileOutputStream fos = new FileOutputStream("output.txt");
       Writer out = new OutputStreamWriter(fos, "UTF8");
       out.write(str);
       out.close();
    } 
    catch (IOException e) {
       e.printStackTrace();
    }
}
In another method, readInput, we read the bytes encoded in UTF8 from the file created by the writeOutput method. We use an InputStreamReader to convert the bytes from UTF8 into Unicode, and return the result in a String. The readInput method is as follows:
static String readInput() {

   StringBuffer buffer = new StringBuffer();
   try {
      FileInputStream fis = new FileInputStream("output.txt");
      InputStreamReader isr = new InputStreamReader(fis, "UTF8");
      Reader in = new BufferedReader(isr);
      int ch;
      while ((ch = in.read()) > -1) {
         buffer.append((char)ch);
      }
      in.close();
      return buffer.toString();
   } 
   catch (IOException e) {
      e.printStackTrace();
      return null;
   }
}
In the main method of our example program, we invoke the writeOutput method to create a file of bytes encoded in UTF. Then we read the same file, converting the bytes back into Unicode. The source code for the main method is:
public static void main(String[] args) {

   String jaString  = 
      new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");

   writeOutput(jaString);
   String inputString = readInput();
   String displayString = jaString + " " + inputString;
   new ShowString(displayString, "Conversion Demo");
}
The original string (jaString) should be identical to the newly created string (inputString). To see if the two strings are the same, we concatenate them and display them with a ShowString object. The ShowString class displays a string with the Graphics.drawString method. The source code for this class is in the ShowString.java. file. When we instantiate ShowString in our sample program, the following window appears. The repetition of the characters displayed verifies that the two strings are identical.


Previous | Next | Trail Map | Internationalization | Converting Non-Unicode Text