The character input stream class hierarchy is shown in Figure 25.3. The Reader class is the abstract superclass of all character input stream classes. Among other things, it defines methods to read one or more characters from the input stream. Figure 25.3. Character input stream classes
The BufferedReader class provides an internal buffer to improve I/O performance. It also defines the readLine() method that can read an entire line of data at a time. The InputStreamReader class is a bridging class between character and byte input streams. It can be wrapped around a byte stream allowing data from the byte stream to be read as characters. The FileReader class connects an input stream to a file so character data can be read from the file. Reader ClassThe Reader class is the abstract superclass of all character input stream classes. It provides methods that are available to all character input streams. The most important of these methods are the close() and read() methods.
The close() method, when overridden by a Reader subclass, closes the input stream and releases any resources associated with it. The read() method is used to read one or more characters from the input stream. The first version reads one character and returns the character as an integer value between 0 and 65535. The second version reads characters into the specified character array. The return value is the number of characters read or “1 if the end of the stream has been reached. The third version does the same thing as the second version except length characters are read into the array starting at array index offset . BufferedReader ClassA BufferedReader provides a memory buffer for more efficient reading of character data. The class also defines the readLine() method that can read an entire line of text from an input file and return the line as a String . A BufferedReader is wrapped around another character input stream. The class defines two public constructors.
The second version of the constructor allows you to specify the buffer size. In addition to overriding some of the methods from the Reader class, the BufferedReader class defines the following method.
The readLine() method reads a line of text from the input stream and returns the line as a String . The read operation will continue until a linefeed , carriage return, or carriage return immediately followed by a linefeed is encountered . To see how a BufferedReader is used to read input data from a file, see the "Reading and Writing to a File" section later in this chapter. FileReader ClassThe FileReader class represents a character input stream that is connected to a file, allowing you to read data from the file. The class does not define any new methods or override any of the methods inherited from the InputStreamReader or Reader classes, but it does define three public constructors.
You can specify the file using a File object, a FileDescriptor object, or by providing a String containing the name and/or path of the file. An example of using the FileReader class is provided in the "Reading and Writing to a File" section later in this chapter. InputStreamReader ClassThe InputStreamReader class provides a bridge between the byte and character input stream worlds . An InputStreamReader object (a character stream) is wrapped around an InputStream object (a byte stream). The InputStreamReader reads the input data as bytes and then converts the bytes to character data using a character set mapping. The InputStreamReader class allows you to do some interesting things. For example, if you wrap an InputStreamReader around the standard input stream, you can read keyboard input as character data. If you wanted to read a line of keyboard input at a time, you could wrap a BufferedReader around the InputStreamReader . The InputStreamReader class does not define any new methods to read data but does define four public constructors.
The first constructor version uses the default character set mapping scheme. The other three versions allow you to specify a character mapping to be used. For an example of using an InputStreamReader to read keyboard input, see the "Using the Standard I/O Streams" section later in this chapter. Other Reader SubclassesThere are other Reader subclasses in the java.io package that you probably won't use much in your scientific and engineering programming work. These include CharArrayReader , FilterReader , PipedReader , PushbackReader , and StringReader . We won't discuss these classes in this chapter. If you want more details on them, consult the Sun Java doc pages. |