Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java SequenceInputStream Class

In this tutorial, we will learn about SequenceInputStream class in Java. This class is used to read data from multiple streams. As the name suggests it reads data sequentially, it means one by one.

Java SequenceInputStream Class Declaration

This is the declaration of SequenceInputStream class and we can see InputStream class is inherited inside this class.

public class SequenceInputStream extends InputStream  

Constructors of SequenceInputStream Class

Constructor Description
SequenceInputStream(InputStream s1, InputStream s2) It creates a new input stream by reading the data of two input stream in order, first s1 and then s2.
SequenceInputStream(Enumeration e) It creates a new input stream by reading the data of an enumeration whose type is InputStream.

Methods of SequenceInputStream Class

Method Description
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.

Java SequenceInputStream Example

In the example given below, we are reading data from two files using SequenceInputStream class. For both the files, we created two objects of FileInputStream for two files and pass them to the constructor of SequenceInputStream class. Now using read() method we read the data of both files one by one. We can see the output of this program is the content of two files.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		FileInputStream fileInputStream1=new FileInputStream("E:\\studytonight\\myfile1.txt");    
		FileInputStream fileInputStream2=new FileInputStream("E:\\studytonight\\myfile2.txt");    
		SequenceInputStream inst=new SequenceInputStream(fileInputStream1, fileInputStream2);    
		int c;    
		while((c=inst.read())!=-1)
		{    
			System.out.print((char)c);    
		}    
		inst.close();    
		fileInputStream1.close();    
		fileInputStream2.close();  
	}  
}


1234567890ABCDEFGHIJKL

myfile1.txt

1234567890

myfile2.txt

ABCDEFGHIJKL

Example that reads the data from two files and writes into another file

In the example given we will read data from two files and store that same data into output.txt file. To read the data we need objects of a FileInputStream class for respective files. To read sequentially from this file we will use SequenceInputStream class and using read() method we will read content from these files and using write() method of FileOutputStram class we will write it to the output.txt file.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		FileInputStream fileInputStream1=new FileInputStream("E:\\studytonight\\myfile1.txt");    
		FileInputStream fileInputStream2=new FileInputStream("E:\\studytonight\\myfile2.txt");    
		FileOutputStream fout=new FileOutputStream("E:\\studytonight\\output.txt");      
		SequenceInputStream sis=new SequenceInputStream(fileInputStream1, fileInputStream2);    
		int i;    
		while((i=sis.read())!=-1)    
		{    
			fout.write(i);        
		}    
		sis.close();    
		fout.close();      
		fileInputStream1.close();      
		fileInputStream2.close();       
		System.out.println("Data is written to the file successfully...");  
	}    
}  


Data is written to the file successfully...

myfile1.txt

1234567890

myfile2.txt

ABCDEFGHIJKL

output.txt

1234567890ABCDEFGHIJKL

Conclusion

In this tutorial, we learned how to read data sequentially from a file using SequenceInputStream class. This class is used to read data from multiple streams. As the name suggests it reads data sequentially, it means one by one.