SequenceInputStream Java

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.


SequenceInputStream Java
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.

6 thoughts on “SequenceInputStream Java

  1. premnath

    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. Avisek Panda

    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. S. Nageswara Rao, Corporate Trainer Post author

      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 Reply

Your email address will not be published. Required fields are marked *