J2EEOnlineJ2EE 





Reading Writing Text  «Prev  Next»
Lesson 3The Reader and Writer classes
ObjectiveExamine the Reader and Writer classes for converting character-based data.

Java Readers and Writers

The java.io.Reader and java.io.Writer classes are abstract superclasses for classes that read and write character-based data.
The subclasses are notable for handling the conversion between different character sets.
Input and output streams are fundamentally byte-based. However, readers and writers are based on characters. In Java, a char is a two-byte Unicode character; but in other character sets that you may have to read or write, characters can have varying widths. ASCII and ISO Latin-1 use one-byte characters. Unicode uses two-byte characters. UTF-8 uses characters of varying width between one and three bytes. Readers and writers know how to handle all these character sets and many more seamlessly.
The encoding and decoding classes themselves are hidden in the sun packages. They are used internally by the InputStreamReader and OutputStreamWriter classes to convert the bytes used by the streams into chars used by the readers and writers and vice versa.
You can also convert byte arrays in a particular character set into Unicode strings using two String constructors.
The exact list of encodings available varies a little from platform to platform. Here are most of the important ones. This same set of encodings is used by readers that convert all bytes they read into Unicode chars.


  1. FileReader: This class is used to read character files. Its read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performance and provide more convenient ways to work with the data.
  2. BufferedReader: This class is used to make lower-level Reader classes like FileReader more efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once and keep this data in a buffer. When you ask for the next character or line of data, it is retrieved from the buffer, which minimizes the number of times that time-intensive, file-read operations are performed. In addition, BufferedReader provides more convenient methods, such as readLine(), that allow you to get the next line of characters from a file.
  3. FileWriter: This class is used to write to character files. Its write() methods allow you to write character(s) or strings to a file. FileWriters are usually wrapped by higher-level Writer objects, such as BufferedWriters or PrintWriters, which provide better performance and higher-level, more flexible methods to write data.
  4. BufferedWriter: This class is used to make lower-level classes like FileWriters more efficient and easier to use. Compared to FileWriters, BufferedWriters write relatively large chunks of data to a file at once, minimizing the number of times that slow, file-writing operations are performed. The BufferedWriter class also provides a newLine() method to create platform-specific line separators automatically.
  5. PrintWriter: This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter with a File or a String), you might find that you can use PrintWriter in places where you previously needed a Writer to be wrapped with a FileWriter and/or a BufferedWriter. New methods like format(), printf(), and append() make PrintWriters very flexible and powerful.

Reader Writer Classes
Every platform has a default character set that's used when no other is explicitly specified. On Windows that's likely to be ISO Latin-1. On the Mac it's likely to be MacRoman.