Java-Concatenating Files

Concatenating Files

It is possible to combine two or more input streams (files) into a single input stream (file). This process is known as concatenation of files and is achieved using the SequencelnputStream class. One of the constructors of this class takes two InputStream objects as arguments and combines them to construct a single input stream.

The following program illustrates the process of concatenation. The program creates two objects of class Filelnputstream for the files “data1.da” and “data2.da“. The two Filelnputstream objects ‘fis1′ and ‘fis2′ are used as arguments to the SequencelnputStream constructor to obtain a single input stream object ‘sis‘. The file sis now contains the contents of ‘fis1′ and ‘fis2′. Then uses a while loop to read all bytes in the ‘sis‘ and write them through the FileInputStream ‘fos‘. Finally, the program reads ‘fos‘ and displays its bytes on the screen.

SequenceInputStream Constructors

Constructor Description
SequenceInputStream(InputStream s1, InputStream s2) Initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.
SequenceInputStream(Enumeration<? extends InputStream> e) Initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream.

SequenceInputStream Methods

Method Description
int read() Reads a byte of data from the underlying input stream.
int read(byte[] b) Reads up to b.length bytes of data from the underlying input stream into an array of bytes.
int read(byte[] b, int off, int len) Reads up to len bytes of data from the underlying input stream into an array of bytes.
long skip(long n) Skips over and discards n bytes of data from the input stream.
void close() Closes the stream and releases any system resources associated with it.

Program

Program Source

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.SequenceInputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        try(FileInputStream fis1 = new FileInputStream("data1.da");
            FileInputStream fis2 = new FileInputStream("data2.da");
            SequenceInputStream sis = new SequenceInputStream(fis1,fis2);
            FileOutputStream fos = new FileOutputStream("data3.da")){
            int getData = 0;
            while((getData = sis.read())!=-1)
            {
                fos.write(getData);
            }
        }catch(IOException ex){
            System.out.println("An I/O Error Occurred");
        }
        
        try(FileInputStream fis = new FileInputStream("data3.da")){
            int getData = 0;
            while((getData = fis.read())!=-1)
            {
                System.out.println(getData);
            }
        }catch(IOException ex){
            System.out.println("An I/O Error Occurred");
        }
    }
}

Leave a Comment