Java Reference
In-Depth Information
This program creates a
FileReader
named
fileIn
to read from the named
file and then inserts a
LineNumberReader
, named
in
, before it.
LineNumber-
Reader
objects get their characters from the reader they are attached to,
keeping track of line numbers as they read. The
getLineNumber
method
returns the current line number; by default, lines are counted starting
from
zero.
When this program is run on itself looking for the letter
'I'
,
its output is
'I' at line 4
You can set the current line number with
setLineNumber
. This could be
useful, for example, if you have a file that contains several sections of
information. You could use
setLineNumber
to reset the line number to 1 at
the start of each section so that problems would be reported to the user
based on the line numbers within the section instead of within the file.
LineNumberReader
is a
BufferedReader
that has two constructors: One takes
a reference to the wrapped stream and the size of the buffer to use,
while the other only takes a reference to the wrapped stream and uses
a default buffer size.
Exercise 20.5
: Write a program that reads a specified file and searches
for a specified word, printing each line number and line in which the
word is found.
20.5.10.
SequenceInputStream
The
SequenceInputStream
class creates a single input stream from reading
one or more byte input streams, reading the first stream until its end
of input and then reading the next one, and so on through the last one.
SequenceInputStream
has two constructors: one for the common case of
two input streams that are provided as the two parameters to the con-
structor, and the other for an arbitrary number of input streams using
Enumeration
is an interface that provides an ordered iteration through a