Java file SequentialInputStream


 

Java file SequentialInputStream

This section demonstrates you the use of class SequentialInputStream.

This section demonstrates you the use of class SequentialInputStream.

Java file SequentialInputStream

This section demonstrates you the use of class SequentialInputStream.

The class SequentialInputStream represents the concatenation of multiple input streams. It starts with an ordered collection of input streams and reads from the first one until end of file is reached, then it reads from the second one, and so on, until the last input streams is reached.

It acts as a single InputStream that represent the streams of other InputStreams in sequence. You can see in the given example, we have created three objects of FileInputStream to parse three different files and then add these to the vector of InputStreams. All these vector elements is then passed to the constructor of SequentialInputStream which reads all the content and display it on the console.

Here is the code:

import java.io.*;
import java.util.*;

public class FileSequentialInputStream {
	public static void main(String[] args) throws Exception {
		FileInputStream f1 = new FileInputStream("C:/output.txt");
		FileInputStream f2 = new FileInputStream("C:/file.txt");
		FileInputStream f3 = new FileInputStream("C:/data.txt");
		Vector vector = new Vector();
		vector.add(f1);
		vector.add(f2);
		vector.add(f3);
		SequenceInputStream sis = new SequenceInputStream(vector.elements());
		int i;
		while ((i = sis.read()) != -1) {
			System.out.write(i);
		}
	}
}

Through the above code, you can easily understand the concept of SequentialInputStream class.

Ads