Java Reference
In-Depth Information
list of objects. For SequenceInputStream , the enumeration should contain
only InputStream objects. If it contains anything else, a ClassCastExcep-
tion will be thrown when the SequenceInputStream TRies to get that object
from the list.
The following example program concatenates all its input to create a
single output. This program is similar to a simple version of the UNIX util-
ity cat if no files are named, the input is simply forwarded to the output.
Otherwise, the program opens all the files and uses a SequenceInputStream
to model them as a single stream. Then the program writes its input to
its output:
import java.io.*;
import java.util.*;
class Concat {
public static void main(String[] args)
throws IOException
{
InputStream in; // stream to read characters from
if (args.length == 0) {
in = System.in;
} else {
InputStream fileIn, bufIn;
List<InputStream> inputs =
new ArrayList<InputStream>(args.length);
for (String arg : args) {
fileIn = new FileInputStream(arg);
bufIn = new BufferedInputStream(fileIn);
inputs.add(bufIn);
}
Enumeration<InputStream> files =
Collections.enumeration(inputs);
in = new SequenceInputStream(files);
}
int ch;
while ((ch = in.read()) != -1)
System.out.write(ch);
 
Search WWH ::




Custom Search