---
title: "SequenceInputStream Class in Java"  
description: "If we want to read data from multiple stream in java input/output operation then SequenceInputStream class is very useful for that."  
author: "Anupam Mishra"  
published: 2015-12-02  
updated: 2018-09-12  
canonical: https://www.mindstick.com/articles/11932/sequenceinputstream-class-in-java  
category: "java"  
tags: ["java", "file", "j2ee"]  
reading_time: 2 minutes  

---

# SequenceInputStream Class in Java

In Java, SequenceInputStream class is used for [reading data](https://www.mindstick.com/forum/12915/how-to-embed-asp-dot-net-with-sql-server-reading-data-and-changing-textbox-field) from [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [stream](https://www.mindstick.com/forum/1901/stream-not-writing-on-a-file) or files.

For example, we have created two different files and taken [two strings](https://www.mindstick.com/forum/159716/regex-match-all-characters-between-two-strings) for [writing](https://www.mindstick.com/articles/13146/6-great-tools-that-will-make-your-writing-shine) one after another of these files with the help of FileOutputStream class. Then we created a one object of SequenceInputStream class for reading existing file and we have write to another file of those contents

## ```
import java.io.*; class SequenceInput{ public static void.
main(String args[])throws Exception{ FileOutputStream
fout1=new FileOutputStream("SequenceInputStream1.txt"); FileOutputStream
fout2=new   FileOutputStream("SequenceInputStream2.txt"); FileInputStream
fin1=new FileInputStream("SequenceInputStream1.txt"); FileInputStream
fin2=new FileInputStream("SequenceInputStream2.txt"); String
s="Sachin Tendulkar";String
s1="Viru"; byte
b[]=s.getBytes();//converting string into byte array fout1.write(b);byte
c[]=s1.getBytes();//converting string into byte array fout2.write(c);FileOutputStream
fout=new   FileOutputStream("SequenceInputStreamFinal.txt"); SequenceInputStream ss=new
SequenceInputStream(fin1,fin2); int i; while((i=ss.read())!=-1) { fout.write(i);     } System.out.println(“Successfully Done!...”)ss.close(); fout.close();   fin1.close();   fin2.close();   }  } 
```

Output:\Successfully Done!...

If we need to read the data from more than [two files](https://www.mindstick.com/forum/161621/how-can-you-compare-two-files-byte-by-byte-to-check-if-they-are-identical)

We need to have theseinformation in the [Enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) object. Enumeration object can be get by callingelements [method](https://www.mindstick.com/forum/166/webservice-method) of the Vector class. Now, we are reading the data from the 4 existing files.

## ```
    import java.io.*; import java.util.*;     class UsingEnum{     public static void main(String args[])throws IOException    {     FileInputStream
 fin1=new FileInputStream("SequenceInputStream1.txt");     FileInputStream
 fin2=new FileInputStream("SequenceInputStream1.txt");     FileInputStream
 fin3=new FileInputStream("Fil.txt");     FileInputStream
 fin4=new FileInputStream("Flood.java");      Vector v=new Vector();     v.add(fin1);     v.add(fin2);     v.add(fin3);     v.add(fin4);      Enumeration e=v.elements(); 
SequenceInputStream bin=new SequenceInputStream(e);     int i=0;  
while((i=bin.read())!=-1){    
System.out.print((char)i);     }     bin.close();     fin1.close();     fin2.close();     }     } 
```

## Output:

##### Sachin Tendulkar

##### Sachin Tendulkar

##### India is a good country I

##### ndia is a good country

---

Original Source: https://www.mindstick.com/articles/11932/sequenceinputstream-class-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
