Advertisement



< Prev
Next >



SequenceInputStream



SequenceInputStream class is used to combine two or more input streams into a single combined input stream and reads data out of this combined input stream.




Constructors :


SequenceInputStream(InputStream is1, InputStream is2)
This constructor takes two InputStream objects in the parameters and combine these two input streams into one input stream for reading.
Example-:

FileInputStream fis1 = new FileInputStream("D:\\TextBook1.txt");
FileInputStream fis2 = new FileInputStream("D:\\TextBook2.txt");

SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
The first FileInputStream reference, fis1 points to file D:\\TextBook1.txt, second FileInputStream reference, fis2 points to D:\\TextBook2.txt. According to the order of FileInputStream references in the call to constructor of SequenceInputStream class, file referenced by fis1 will be read before the file referenced by fis2.




Point to remember


FileIntputStream is a subclass of InputStream




Methods for reading SequenceInputStream


Some methods that we generally use while working with SequenceInputStream are shown in the table below :

Methods Description
available()
This method gives the remaining number of bytes available to read from this input stream.

read()
This method reads one byte at a time from this input stream.

read(byte[])
This method reads a whole byte array at a time from this input stream.

close()
This method closes this input stream and also frees any resources connected with this input stream.






Reading a sequence of two files using SequenceInputStream



Let's say a file TextBook1.txt already exists in D: drive, with the contents -: Let us learn
Let's say a file TextBook2.txt already exists in D: drive, with the contents -: -Java.

//Program to create a SequenceInputStream to combine two streams

import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
FileInputStream fis1= new FileInputStream("D:\\TextBook1.txt");
FileInputStream fis2= new FileInputStream("D:\\TextBook2.txt");


SequenceInputStream sis= new SequenceInputStream(fis1,fis2); //combining the two input streams

int c;

while( (c=sis.read())!=-1)
{
System.out.print((char)c);
}

bos.flush();

fis1.close();
fis2.close();                                                                         
sis.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}



Output is -:


 Let us learn-Java


Program Analysis





Advertisement




Combining two files into one file.


First InputStream object, fis1 points to file "D://TextBook1.txt" and second InptutStream object, fis2 points to "D://TextBook2.txt". This code will use SequenceInputStream class to make a sequence of these two InputStream objects through which data will be read.
A file "TextBook1.txt" already exists in D: drive, with the contents -: Let us learn-
A file "TextBook2.txt" already exists in D: drive, with the contents -: Java.

//Program to create a SequenceInputStream to combine two streams

import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
FileInputStream fis1= new FileInputStream("D:\\TextBook1.txt");
FileInputStream fis2= new FileInputStream("D:\\TextBook2.txt");


SequenceInputStream sis= new SequenceInputStream(fis1,fis2); //combining the two input streams


FileOutputStream fos= new FileOutputStream("TextBook3.txt");
BufferedOutputStream bos= new BufferedOutputStream(fos);

int c;

while( (c=sis.read())!=-1)
{
System.out.print((char)c);
bos.write(c);
}

bos.flush();

fis1.close();
fis2.close();                                                                         
sis.close();
bos.close();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}


Output


After the execution of this program, a file "TextBook3.txt" is created in the D: drive, with contents -:
Let us learn-Java.


Program Analysis





Please share this article -




< Prev
Next >
< BufferedOutputStream
DataInputStream >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement