Readers
A new suite of character-based input and output streams were introduced in the Java 1.1 release. Reading and writing localized text and converting to Java Unicode characters requires knowledge of the locale in which the program is running. Since Java uses the Unicode encoding for its characters and strings, any character of any commonly-used modern written language is representable in a Java program. "Internationalizing" your programs starts with using the Unicode character I/O streams rather than the byte I/O streams.
Analogous to the InputStream class hierarchy is the Unicode Reader class hierarchy shown below. Note that many class names are similar to the InputStream classes discussed a few pages ago.
The InputStreamReader class is used to convert a binary stream ( such as System.in ) into a character based stream. BufferedReader is used to provide efficiency and some convenience methods for processing character input. In the BufferedReader class, the read() method reads a single character and the readLine() method reads a line of text. The combination of these two classes can help create a console input program based around the following statements:
BufferedReader bufread = new BufferedReader( new InputStreamReader( System.in )); String input = bufread.readLine();
The FileReader class implements a character-based stream connection to a file much like FileInputStream makes a binary stream connection to a file. The constructors for FileReader are:
Here's an example using the FileReader. Note the familiar nesting of the streams.
import java.io.*; class FileReaderExample { public static void main (String args[]) throws IOException, FileNotFoundException { String line; BufferedReader br = new BufferedReader( new FileReader("c:/mydirectory/myfile")); do { line = br.readLine(); if( line != null ) System.out.println(line); } while (line != null); } }
Next... page 7-17