Java SequenceInputStream class example
Java Sample Codes

Java SequenceInputStream Class Example

Java SequenceInputStream class is little bit confusing for the beginners in java programming. This tutorial is a teaching aid for the beginners to know about the usage of Java SequenceInputStream class.

Java SequenceInputStream class is one of the class in java.io package. SequenceInputStream class is used to read data from multiple streams. This class allows you to combine multiple InputStreams and reads data of combined InputStreams one by one. That is it reads data one by one (sequentially) from different input streams.

Working of Java SequenceInputStream class  

Since we can combine multiple InputSreams using Java SequenceInputStream class, it reads from the first InputStreams until end of file is reached, whereupon it reads from the second InputStream, and so on, until end of file is reached on the last InputStreams.

Java SequenceInputStream Example

Here, assume that you have two files: file1.txt and file2.txt which have following information:

file1.text contains

Java SequenceInputStream Example file 1

file2.text contains

Java SequenceInputStream Example file 2 

Example that read and display the data from two files

In this example, we are going explain how to read data of two files file1.txt and file2.txt and display it in the console.

Create a file called SequenceInputStreamExample.java

SequenceInputStreamExample.java

import java.io.*;

class SequenceInputStreamExample{

public static void main(String[] args) throws IOException{

InputStream input1 = new FileInputStream("file1.txt");

InputStream input2 = new FileInputStream("file2.txt");

SequenceInputStream sequenceInputStream = new SequenceInputStream(input1, input2);

int data = sequenceInputStream.read();

while(data != -1){

System.out.print((char)data);

data = sequenceInputStream.read();

}

}

}

Output :

Java SequenceInputStream class example
                                                          Output of Java SequenceInputStream

web resources :

 

Leave a Reply

Your email address will not be published.