Java SequenceInputStream Example

By Arvind Rai, November 19, 2023
Java SequenceInputStream concatenates more than one InputStream . It reads first file till end and then second and so on and finally it gives out one InputStream concatenating all.

Example with Two Files

If we have only two files, the below constructor can be used.
SequenceInputStream(InputStream s1, InputStream s2) 
Find the complete example. Suppose we have two files as below.
D:/cp/file1.txt
This message is from file one.

D:/cp/file2.txt
 this message is from file two.

SequenceInputStreamDemoOne.java
package com.cp.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class SequenceInputStreamDemoOne {
  public static void main(String[] args) throws IOException {
	  FileInputStream fis1 = null;
	  FileInputStream fis2 = null;
	  SequenceInputStream sis = null;
	  try {
		fis1 = new FileInputStream(new File("D:/cp/file1.txt"));
		fis2 = new FileInputStream(new File("D:/cp/file2.txt"));
		sis = new SequenceInputStream(fis1, fis2);
		int i=0;
		while((i=sis.read())!=-1){
			System.out.print((char)i);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		fis1.close();
		fis2.close();
		sis.close();
	}
  }
} 
Output will be as below.
This message is from file one and this message is from file two 

Example with Multiple Files

If we have more than one files, we can choose below constructor.
SequenceInputStream(Enumeration<? extends InputStream> e)  
This uses Enumeration. Add your all files in vector and get the enumeration instance from that vector. For the example find one more file.
D:/cp/file3.txt
 This message is from file three. 

SequenceInputStreamDemoTwo.java
package com.cp.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class SequenceInputStreamDemoTwo {
	public static void main(String[] args) throws IOException {
		  FileInputStream fis1 = null;
		  FileInputStream fis2 = null;
		  FileInputStream fis3 = null;
		  SequenceInputStream sis = null;
		  try {
			fis1 = new FileInputStream(new File("D:/cp/file1.txt"));
			fis2 = new FileInputStream(new File("D:/cp/file2.txt"));
			fis3 = new FileInputStream(new File("D:/cp/file3.txt"));
			
			//add FileInputStream files in vector
			Vector<FileInputStream> vector = new Vector<FileInputStream>();
			vector.add(fis1);
			vector.add(fis2);
			vector.add(fis3);
			Enumeration<FileInputStream> enu = vector.elements();
			
			//pass Enumeration to SequenceInputStream
			sis = new SequenceInputStream(enu);
			int i=0;
			while((i=sis.read())!=-1){
				System.out.print((char)i);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			fis1.close();
			fis2.close();
			fis3.close();
			sis.close();
		}
	  }
} 
Output is given as below.
This message is from file one and this message is from file two. This message is from file three. 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us