SequenceInputStream
in Java is a class used to combine two or more input streams into one so they can be read sequentially, one after the other, as if they were a single continuous stream.
📦 Package
It is part of java.io
package:
import java.io.SequenceInputStream;
🧠 Use Case
Imagine you have multiple files or streams, and you want to read them one after another in a single loop — SequenceInputStream
helps with that.
🔧 Constructors
- Two streams:
SequenceInputStream(InputStream s1, InputStream s2)
Reads from s1
until it ends, then from s2
.
- Enumeration of streams:
SequenceInputStream(Enumeration<? extends InputStream> e)
Reads from a list of InputStreams provided as an Enumeration
.
✅ Example
Combine two files into one stream:
FileInputStream file1 = new FileInputStream("file1.txt");
FileInputStream file2 = new FileInputStream("file2.txt");
SequenceInputStream sis = new SequenceInputStream(file1, file2);
int data;
while ((data = sis.read()) != -1) {
System.out.print((char) data);
}
sis.close();
file1.close();
file2.close();
🧵 Summary
- Combines multiple input streams.
- Reads them in sequence.
- Useful for reading multiple files/streams as a single stream.