Way2Java

SequenceInputStream Java

SequenceInputStream Java is used to copy a number of source files to one destination file. Many input streams are concatenated and converted into a single stream and copied at a stretch to the destination file.

Merging of multiple files can be done easily in Java using SequenceInputStream. SequenceInputStream is a good example to show that Java is a production language where with less code a greater extent of functionality is realized.

Two programs are given using the two overloaded constructors of SequenceInputStream that take two input stream objects as parameters (used to merge two files only) and the other an object of java.util.Enumeration interface (used to merge a number of files, more than 2).

Example on SequenceInputStream Java. Just Merging of Two Files

In the following program, take any two files existing in the same directory and pass them to two different FileInputStream constructors. The contents of these two source files are displayed at DOS prompt and also copied to a destination file.

import java.io.*;
public class TwoFiles
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fistream1 = new FileInputStream("Sayings.txt");  // first source file
    FileInputStream fistream2 = new FileInputStream("Morals.txt");  //second source file

    SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);  
    FileOutputStream fostream = new FileOutputStream("Result.txt");        // destination file

    int temp;
    while( ( temp = sistream.read() ) != -1)
    {
      System.out.print( (char) temp ); // to print at DOS prompt
      fostream.write(temp);	// to write to file
    }
    fostream.close();
    sistream.close();
    fistream1.close();
    fistream2.close();
   }
}



Output screen on SequenceInputStream Java

SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);

Two file handles, fistream1 and fistream2 are passed as parameters to SequenceInputStream constructor. These two objects represent two files, Sayings.txt Morals.txt.

sistream.read()

The read() method reads from the two source files one after another.

SequenceInputStream Java

Merging multiple Files using Enumeration

Following is the second program on SequenceInputStream where three files are merged. The constructor takes an Enumeration object as parameter.

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
public class MultipleFiles
{
  public static void main(String args[]) throws IOException
  {
    FileInputStream fistream1 = new FileInputStream("Sayings.txt");  
    FileInputStream fistream2 = new FileInputStream("Morals.txt");   
    FileInputStream fistream3 = new FileInputStream("abc.txt");   
                                    
    Vector vect = new Vector();
    vect.addElement(fistream1);
    vect.addElement(fistream2);
    vect.addElement(fistream3);
                             // obtain Enumeration object from Vector
    Enumeration e = vect.elements();   
                             // pass Enumeration object to SequenceInputStream constructor
    SequenceInputStream sistream = new SequenceInputStream(e); 
 		             // destination file
    FileOutputStream fostream = new FileOutputStream("bbb.txt");

    int temp;
    while( ( temp = sistream.read() ) != -1)
    {
      System.out.print( (char) temp );    // prints at DOS prompt
      fostream.write(temp);               // prints in the destination file
    }
    fostream.close(); 
    sistream.close();
    fistream1.close();   fistream2.close();   fistream3.close();
  }
}

With many source files, a different approach of coding is followed. Multiple FileInputStream objects, fistream1, fistream2 and fistream3 are created, each opening one source file. All the FileInputStream handles (objects) are added to a Vector object, vect. Using elements() method of Vector, an object Enumeration is obtained and this Enumeration object is passed to the SequenceInputStream constructor.

To write to a file, a destination file, bbb.txt, is opened with FileOutputStream object, fostream.

sistream.read()

Now, the SequenceInputStream object sistream reads three files. It reads a byte and returns it as an ASCII integer value like as in FileToFile1.java program. The ASCII value returned is written to the destination file and converted back to char to write to the DOS prompt.