SequenceInputStream to read data from multiple files (two or more) and writing it in one file in java (i.e. merge files)



The SequenceInputStream introduced in Java 1 can be used to read data from multiple InputStream’s one by one in sequence. Hence SequenceInputStream combines multiple InputStream's into one.

We will read c:/myFile1.txt >
You are learning File IO


and c:/myFile2.txt >
from javaMadeSoEasy.com



Program 1 to Read from two files using SequenceInputStream


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
/** JavaMadeSoEasy.com */
public class sequenceInputStreamReadFromTwoFiles {
   public static void main(String[] args) throws IOException {
          String file1 ="C:/myFile1.txt";
          String file2 ="C:/myFile2.txt";
          InputStream fis1 = new FileInputStream(file1);
          InputStream fis2 = new FileInputStream(file2);
          InputStream sis = new SequenceInputStream(fis1, fis2);
          System.out.println("Read data from "+file1+" & "
                       +file2 +" using SequenceInputStream");
         
          int ch;
          while ((ch = sis.read()) != -1) { // read till end of file of both files
                 System.out.print((char) ch);
          }
          sis.close(); // closing SequenceInputStream
                                     //will close fis1 and fis2 as well
   }
}
/*
Read data from C:/myFile1.txt & C:/myFile2.txt using SequenceInputStream
You are learning File IO from javaMadeSoEasy.com
*/



Program 2 to Read from more than two files using SequenceInputStream


c:/myFile3.txt looks like this >
file3 data

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Vector;
/** JavaMadeSoEasy.com */
public class sequenceInputStreamReadFromMoreThanTwoFiles {
   public static void main(String[] args) throws IOException {
          String file1 ="C:/myFile1.txt";
          String file2 ="C:/myFile2.txt";
          String file3 ="C:/myFile3.txt";
         
          InputStream fis1 = new FileInputStream(file1);
          InputStream fis2 = new FileInputStream(file2);
          InputStream fis3 = new FileInputStream(file3);
          //Create vector which will be used to hold multiple inputStreams
          Vector<InputStream> inputStreams = new Vector<>();
          inputStreams.add(fis1);
          inputStreams.add(fis2);
          inputStreams.add(fis3);
          //Now add inputStreams.elements() to the Vector,
          //inputStreams.elements() will return Enumerations
          InputStream sis =
          new SequenceInputStream(inputStreams.elements());
         
          System.out.println("Reading data from multiple files using SequenceInputStream");
          int ch;
          while ((ch = sis.read()) != -1) { // read till end of file of both files
                 System.out.print((char) ch);
          }
          sis.close(); // closing SequenceInputStream
                                     //will close fis1, fis2 and fis3 as well
   }
}
/*
Reading data from multiple files using SequenceInputStream
You are learning File IO from javaMadeSoEasy.com file3 data
*/



Application of SequenceInputStream >
SequenceInputStream when can used when we want to read data from multiple files and write it in one file.



Program 3 to Read from two files and writing it in one file using SequenceInputStream


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
/** JavaMadeSoEasy.com */
public class sequenceInputStreamReadFromTwoAndWriteInOneFile {
   public static void main(String[] args) throws IOException {
          String file1 ="C:/myFile1.txt";
          String file2 ="C:/myFile2.txt";
          InputStream fis1 = new FileInputStream(file1);
          InputStream fis2 = new FileInputStream(file2);
          InputStream sis = new SequenceInputStream(fis1, fis2);
         
          String outputFileName = "c:/myFileSequenceInputStream.txt";
          //File in which we will write data from two files
          OutputStream os = new FileOutputStream(outputFileName);
          int ch;
          while ((ch = sis.read()) != -1) { // read till end of file of both files
                 os.write(ch);
          }
          System.out.println("Data from "+file1+" & " +file2 +
                       " has been written in "+outputFileName+" using SequenceInputStream");
          sis.close(); // closing SequenceInputStream
                                     //will close fis1 and fis2 as well
          os.close(); //close FileOutputStream
   }
}
/*
Data from C:/myFile1.txt & C:/myFile2.txt has been written in c:/myFileSequenceInputStream.txt using SequenceInputStream
*/




Closing SequenceInputStream? Do we need to close InputStream’s (like FileInputStream’s) which have been wrapped by SequenceInputStream?


No, SequenceInputStream is wrapper around FileInputStream and closing wrapper (i.e. SequenceInputStream) will close SequenceInputStream and FileInputStream as well.


In java 7, using Try-with-resources we need not to write explicit code for closing file.


Program 4 to use SequenceInputStream with Try-with-resources.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
/** JavaMadeSoEasy.com */
public class sequenceInputStreamTryWithResource {
   public static void main(String[] args) throws IOException {
          String file1 = "C:/myFile1.txt";
          String file2 = "C:/myFile2.txt";
          InputStream fis1 = new FileInputStream(file1);
          InputStream fis2 = new FileInputStream(file2);
          // Try-with-resource
          try (InputStream sis = new SequenceInputStream(fis1, fis2)) {
                 int ch;
                 while ((ch = sis.read()) != -1) {
                       System.out.print((char) ch);
                 }
          }
   }
}



RELATED LINKS>

Try-with-resources in java - java.lang.AutoCloseable interface

eEdit
Must read for you :