Character Input Streams


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

graphics/25fig03.gif

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 Class

The 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.

[View full width]
 
[View full width]
public abstract void close() throws IOException public int read() throws IOException public int read(char[] chars) throws IOException public int read(char[] chars, int offset, int length) throws graphics/ccc.gif IOException

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 Class

A 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.

 public BufferedReader(Reader stream) public BufferedReader(Reader stream, int bufferSize) 

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.

 public String readLine() throws IOException. 

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 Class

The 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.

 public FileReader(File file) throws FileNotFoundException public FileReader(FileDescriptor desc) public FileReader(String name) throws FileNotFoundException 

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 Class

The 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.

[View full width]
 
[View full width]
public InputStreamReader(InputStream stream) public InputStreamReader(InputStream stream, Charset cs) public InputStreamReader(InputStream stream, CharsetDecoder dec) public InputStreamReader(InputStream stream, String charsetName) graphics/ccc.gif throws UnsupportedEncodingException

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 Subclasses

There 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.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net