Open In App

Java.io.SequenceInputStream in Java

The SequenceInputStream class allows you to concatenate multiple InputStreams. It reads data of streams one by one. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams.

Constructor and Description



Important Methods:

The following is an example of SequenceInputStream class that implements some of the important methods.
Program:




//Java program to demonstrate SequenceInputStream
import java.io.*;
import java.util.*;
  
class SequenceISDemp
{
    public static void main(String args[])throws IOException
    {
  
        //creating the FileInputStream objects for all the following files
        FileInputStream fin=new FileInputStream("file1.txt");
        FileInputStream fin2=new FileInputStream("file2.txt");
        FileInputStream fin3=new FileInputStream("file3.txt");
  
        //adding fileinputstream obj to a vector object
        Vector v = new Vector();
          
        v.add(fin);
        v.add(fin2);
        v.add(fin3);
          
        //creating enumeration object by calling the elements method
        Enumeration enumeration = v.elements();
  
        //passing the enumeration object in the constructor
        SequenceInputStream sin = new SequenceInputStream(enumeration);
          
        // determine how many bytes are available in the first stream
        System.out.println("" + sin.available());
          
        // Estimating the number of bytes that can be read 
        // from the current underlying input stream 
        System.out.println( sin.available());
          
        int i = 0;
        while((i = sin.read())! = -1)
        {
            System.out.print((char)i);
        }
        sin.close();
        fin.close();
        fin2.close();
        fin3.close();
    }
}

Output:



19
This is first file This is second file This is third file

Note: This program will not run on online IDE as there are no files associated with it.


Article Tags :