SequenceInputStream Java

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();
  }
}

SequenceInputStream Java

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.

6 thoughts on “SequenceInputStream Java”

  1. How to merge two pdf files. while I follow the above code. In the output file, I am getting the 2nd file content only. But the file size is sum of the two files.

    Below is the code snippet.

    list.add(new FileInputStream(new File(“D:/Premnath/upload/1.pdf”)));
    list.add(new FileInputStream(new File(“D:/Premnath/upload/2.pdf”)));
    list.add(new FileInputStream(new File(“D:/Premnath/upload/3.pdf”)));
    SequenceInputStream fis = new SequenceInputStream(Collections.enumeration(list));

    OutputStream output = new FileOutputStream(new File(“D:/Premnath/upload/result.pdf”));

    byte[] buffer = new byte[600];
    int bytesRead = 0;
    // System.out.println(“before”);
    while ((bytesRead = sis.read(buffer)) != -1) {
    // System.out.println(bytesRead);
    output.write(buffer, 0, bytesRead);
    }
    // System.out.println(“after”);

  2. I have two text files.
    1st text files contains records like–
    ajdgetekcsdo923410
    ajdgetekcsdo923410
    ajdgetekcsdo923410

    2nd text files have error messages related to the records present in 1st text file—
    error due to ……
    error due to……
    error due to……

    I want to mergr these two datas into one file delimited by pipes and at the end it should have 15 spaces for e.g—

    ajdgetekcsdo923410|error due to ……| |
    ajdgetekcsdo923410|error due to ……| |
    ajdgetekcsdo923410|error due to ……| |

    please help me out in solving this….

    1. import java.io.*;
      public class Merge
      {
      public static void main(String args[]) throws IOException
      {
      BufferedReader br1 = new BufferedReader(new FileReader(“Source1.txt”));
      BufferedReader br2 = new BufferedReader(new FileReader(“Source2.txt”));

      StringBuffer sb = new StringBuffer();
      while(true)
      {
      try
      {
      String str1 = br1.readLine();
      if(str1!= null)
      {
      sb.append(str1);
      sb.append(“|”);
      sb.append(br2.readLine());
      sb.append(” ||\n”); // after 15 spaces || comes
      }
      else
      {
      break;
      }
      }
      catch(EOFException e)
      {
      br1.close();
      br2.close();
      break;
      }
      }
      System.out.println(sb);
      }
      }

      It is just a rough work to give an idea. It is considered both source files contain the same number of lines. What is not considered is a) performance b) less RAM space c) not written to a file the contents of sb etc. This (customization) you can do I think.

Leave a Comment

Your email address will not be published.