SequenceInputStream class

SequenceInputStream class concatenates multiple InputStreams.

Constructors from SequenceInputStream:

SequenceInputStream(InputStream first, InputStream second)
a pair of InputStreams
SequenceInputStream(Enumeration <? extends InputStream> streamEnum)
an Enumeration of InputStreams

SequenceInputStream reads from the first InputStream until it runs out and then switches over to the second one.

Revised from Open JDK source code


import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class Main {
  public static void main(String args[]) throws IOException {
    SequenceInputStream inStream;
    FileInputStream f1 = new FileInputStream("file1.java");
    FileInputStream f2 = new FileInputStream("file2.java");
    inStream = new SequenceInputStream(f1, f2);
    boolean eof = false;
    int byteCount = 0;
    while (!eof) {
      int c = inStream.read();
      if (c == -1)
        eof = true;
      else {
        System.out.print((char) c);
        ++byteCount;
      }
    }
    System.out.println(byteCount + " bytes were read");
    inStream.close();
    f1.close();
    f2.close();
  }
}
Home 
  Java Book 
    File Stream