Java Reference
In-Depth Information
}
// ...
}
If there are no parameters, we use
System.in
for input. If there are para-
meters, we create an
ArrayList
large enough to hold as many
BufferedIn-
putStream
objects as there are command-line arguments (see "
ArrayList
"
the stream to the
inputs
list. When the loop is finished, we use the
Col-
lections
class's
enumeration
method to get an
Enumeration
object for the
list elements. We use this
Enumeration
in the constructor for
SequenceIn-
putStream
to create a single stream that concatenates all the streams for
the files into a single
InputStream
object. A simple loop then reads all the
bytes from that stream and writes them on
System.out
.
You could instead write your own implementation of
Enumeration
whose
nextElement
method creates a
FileInputStream
for each argument on de-
mand, closing the previous stream, if any.
20.5.11.
Pushback
Streams
A
Pushback
stream lets you push back, or "unread," characters or bytes
when you have read too far.
Pushback
is typically useful for breaking in-
put into tokens. Lexical scanners, for example, often know that a token
(such as an identifier) has ended only when they have read the first
character that follows it. Having seen that character, the scanner must
push it back onto the input stream so that it is available as the start of
the next token. The following example uses
PushbackInputStream
to report
the longest consecutive sequence of any single byte in its input:
import java.io.*;
class SequenceCount {
public static void main(String[] args)
throws IOException