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.
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”);
See my example. I have not taken an array to copy. Anyhow check your code in while loop.
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….
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.
nice,, but i need alternate merging of two files
Open FileOutputStream in append mode. Copy two times with different FileInputStream objects in two different while loops.