Declaration
public class SequenceInputStream extends InputStream
Working of SequenceInputStream
This input stream first reads the data(in bytes) from the first stream sequentially. Then all the data sequentially from the second stream, from the third, and so on.
While ending the first stream, the file is reached at a closed state; then, the following data comes from the input stream. This process continues until the file is reached at the end of the input streams.
Constructors for SequenceInputStream

Methods of SequenceInputStream

Examples
File contents of input.txt
Hello Coders
File contents of out.txt
Welcome to Coding Ninjas.
Code 1
import java.io.*;
public class SequenceInput1 {
public static void main(String args[])throws Exception{
FileInputStream input1=new FileInputStream("input.txt");
FileInputStream input2=new FileInputStream("out.txt");
SequenceInputStream inst=new SequenceInputStream(input1, input2);
int j;
while((j=inst.read())!=-1){
System.out.print((char)j);
}
inst.close();
input1.close();
input2.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Hello Coders.
Welcome to Coding Ninjas.

You can also try this code with Online Java Compiler
Run Code
In this example, we have taken SequenceInputStream, and we have input the two files from the user and printed the contents of the file.
File contents of input.txt
Hello Coders
File contents of out.txt
Welcome to Coding Ninjas.
Practice by yourself on java online compiler.
Code 2
import java.io.*;
public class SequenceInput2
{
public static void main(String[] args) throws IOException
{
FileInputStream file1 = new FileInputStream("input.txt");
FileInputStream file2 = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("out.txt");
SequenceInputStream file3 = new SequenceInputStream(file1,file2);
BufferedInputStream bis = new BufferedInputStream(file3);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int i;
while((i = bis.read()) != -1){
bos.write((char)i);
}
bis.close();
bos.close();
fos.close();
file1.close();
file2.close();
file3.close();
System.out.println("Successfully written...");
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Successfully written…

You can also try this code with Online Java Compiler
Run Code
In this example, we have taken two different files and tried as an input, and we also used both files to write the content on the third file. After validating certain conditions, we have printed a message written on the third file is successful.
Check out this problem - Shortest Common Supersequence, Duck Number in Java
FAQs
-
What is a SequenceInputStream?
It represents the logical concatenation of other input streams. This begins on the ordered collection of input streams and reads from the first one until the end of the file is encountered.
-
How to read data from multiple files in Java SequenceInputStream?
If we want to read the data from multiple files, we need to use Enumeration. It can be obtained by calling the elements() method.
-
How to create a new InputStream in Java?
Java.io is used to create SequenceInputStream class. It creates a new Input stream by reading the data of two input streams in order, first s1 then s2.
Key Takeaways
In this article section, we have discussed the topic of SequenceInputStream. We briefly introduced the topic and explained the declaration. We also discussed its working along with methods and constructors used in it. We also took a few examples regarding the topic. I hope that this topic might enhance your knowledge, and it might have eased your coding journey. If you want to explore more topics and practice more, please visit Top Apple coding Interview Questions. Do upvote our blog to help other ninjas grow. Happy Coding!”