Reader Wrappers

There is the usual variety of wrapper classes that can wrap a Reader, shown in the Figure 17-4.

Figure 17-4. Wrapping the Reader classes

image

Classes that wrap readers

The classes that wrap a Reader are:

  • BufferedReader. This class can provide a performance boost, and also has a readLine() method. The BufferedReader needs to wrap the class that actually accesses the data (e.g., the FileReader or whatever). Other classes may be layered on top of the BufferedReader, too.

  • FilterReader. You subclass FilterReader, and your overriding methods allow you to see and modify individual characters as they come in—before the rest of your program sees them.

  • LineNumberReader. This class keeps track of the line number count on this stream. You can find out the input line you are currently on by calling getLineNumber(). This class doesn't really offer enough value to justify its existence. It was written to support the first Java compiler and included in the API for no better reason than “Hey, we already had it written”.

  • PushbackReader. This class maintains an internal buffer that allows characters to be “pushed back” into the stream after they have been read, allowing the next read to get them again. The default buffer size is one character, but there is a constructor that lets you specify a larger size. You might use this if you were assembling successive characters into a number and you come to a character that can't be part of a number. You will push it back into the input stream so it can be ignored, but kept available for the next read attempt. This is less necessary now that we have the Scanner class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118