Java SequenceInputStream Class

Last Updated : 23 Jan 2026

The SequenceInputStream class in Java is used to read data sequentially from multiple input streams as if they were a single stream.

In this chapter, we will learn what the SequenceInputStream class is, its declaration, constructors, methods, and how to read data from multiple files using different examples.

What is SequenceInputStream in Java?

The SequenceInputStream class is used to read data from two or more input streams one after another. It reads the data from the first stream completely and then continues reading from the next stream.

This class is useful when data is divided into multiple files and needs to be read in a continuous sequence.

SequenceInputStream Class Declaration

The following declaration shows how the SequenceInputStream class is defined:

Constructors of SequenceInputStream Class

The SequenceInputStream class provides constructors to read data from multiple input streams.

1. SequenceInputStream(InputStream s1, InputStream s2)

This constructor creates a new input stream that reads data first from s1 and then from s2.

Syntax:

Here is the syntax of this constructor:

2. SequenceInputStream(Enumeration e)

This constructor creates a new input stream that reads data from multiple input streams provided through an Enumeration.

Syntax:

Here is the syntax of this constructor:

Methods of SequenceInputStream Class

The SequenceInputStream class provides methods to read data from combined streams.

MethodDescription
int read()It is used to read the next byte of data from the input stream.
int read(byte[] ary, int off, int len)It is used to read len bytes of data from the input stream into the array of bytes.
int available()It is used to return the maximum number of byte that can be read from an input stream.
void close()It is used to close the input stream.

Examples of SequenceInputStream Class

The following examples demonstrate how to use the SequenceInputStream class in different scenarios.

Example 1: Reading Data from Two Files Sequentially

This example demonstrates how to read data from two files one after another using SequenceInputStream.

Here, we are assuming that you have two files: testin.txt and testout.txt which have following information:

testin.txt:

Welcome to Java IO Programming.

testout.txt:

It is the example of Java SequenceInputStream class.

After executing the program, you will get following output:

Output:

Welcome to Java IO Programming. It is the example of Java SequenceInputStream class.

Example 2: Reading from Two Files and Writing into Another File

This example demonstrates how to read data from two files and write the combined data into another file.

Output:

Succeess...

testout.txt:

Example 3: Reading Data from Multiple Files Using Enumeration

This example demonstrates how to read data from more than two files using SequenceInputStream with Enumeration.

The a.txt, b.txt, c.txt and d.txt have following information:

a.txt:

Welcome

b.txt:

to

c.txt:

java

d.txt:

programming

Output:

Welcometojavaprogramming