Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

The java.io Package - Readers and Writers

InputStreamReader

  • InputStreamReader extends Reader and has one subclass, FileReader
  • InputStreamReader reads bytes and translates them to Unicode characters using the specified character encoding or the default system encoding
  • the class has two constructors
        InputStreamReader(InputStream in)
        InputStreamReader(InputStream in, String enc)
    
  • to use an InputStreamReader you must first create an instance of it for a byte input stream. You can then read the stream using any of the Reader methods.

OutputStreamWriter

  • OutputStreamWriter extends Writer and has one subclass, FileWriter
  • OutputStreamWriter translates between Unicode characters and bytes using the specified character encoding or the default system encoding
  • the class also has two constructors
        OuputStreamWriter(OutputStream out)
        OuputStreamWriter(OutputStream out, String enc)
    
  • you use OutputStreamWriter by first creating an instance of it for a byte output stream; you can then write to the stream using an Writer methods.

Character Encoding

  • Character encodings specify how 8-bit bytes are translated to 16-bit Unicode
  • they are represented by Strings which follow the naming standards set by IANA Character Registry
  • every implementation of Java is required to support the following sets:
      US-ASCII     Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the
                   Basic Latin block of the Unicode character
                   set
      ISO-8859-1  
                   ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
      UTF-8
                   Eight-bit Unicode Transformation Format
      UTF-16BE
                   Sixteen-bit Unicode Transformation Format,
                   big-endian byte order
      UTF-16LE
                   Sixteen-bit Unicode Transformation Format,
                   little-endian byte order
      UTF-16
                   Sixteen-bit Unicode Transformation Format,
                   byte order specified by a mandatory initial
                   byte-order mark (either order accepted on
                   input, big-endian used on output)
    
  • specific platforms ie those used in Japan, China, Mid-East, etc, may include other encodings
  • the streams are used to read and write data encoded in a character set which is different than the default system encoding
  • For example (JPL pg238), to read bytes encoded under ISO 8859-6 for Arabic characters
    public Reader readArabic(String file) throws IOException {
        InputStream fileIn = new FileInputSgream(file);
        return new InputStreamReader(fileIn, "iso-8859-6");
    }
    


Pkg Overview Data Streams Character Streams Byte Streams File Class Readers & Writers
Filter Streams Data Input/Output Reading & Writing Files Serialization