Home Java Example Java Io Java IO SequenceInputStream Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO SequenceInputStream Example

Advertisement
In this tutorial we will learn about the SequenceInputStream class.

Java IO SequenceInputStream Example

In this tutorial we will learn about the SequenceInputStream class.

SequenceInputStream class is provided in the java.io package. It is a sub class of InputStream which facilitate to concatenate the other input streams logically. This class concatenate the input stream in the sequence of files in which they are ordered i.e. reads the first file until reached at end the of file and then reads another file until reached at the end of file and so on.

Constructors of SequenceInputStream

  • SequenceInputStream(Enumeration<? extends InputStream> e)
  • SequenceInputStream(InputStream s1, InputStream s2)

Commonly used methods of SequenceInputStream

  • available() : Readable number of bytes are returned by this method.
  • close() : This method is used to close the input stream and it releases the resources used by the stream.
  • read() : This method is used to read the bytes from the input stream.
  • read(byte[] b, int off, int len) :  This method is used to read the specified length 'len' of bytes from the specified byte array 'b' started form the offset 'off'.

Example :

An example of SequenceInputStream is being given below which will demonstrate you about how to merger or concatenate the contents of two files. In this example I have created two text files and write some texts into the both of these files. Then I have created a Java class named SequenceInputStreamExample and to read the input stream created the two references of InputStream 'is' and 'is1' and an instance of FileInputStream which reads the data input stream from the text files correspondingly (see in example). Then uses the constructor SequenceInputStream(InputStream s1, InputStream s2) to merge the contents of the files. In this case file1 will be read until reached at the end of file and then started to read contents of other file until reached at end of file. At last displayed all the files at the console.

/*This example demonstrates how to merge
the two files using SequenceInputStream*/

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

public class SequenceInputStreamExample
{
  public static void main(String args[])
   {
      InputStream is = null;
      InputStream is1 = null;
      try
         {
            is = new FileInputStream("read.txt");
            is1 = new FileInputStream("read1.txt");
            SequenceInputStream sis = new SequenceInputStream(is, is1);
            int c;
            System.out.println();
            while((c = sis.read()) != -1)
             {
                System.out.print((char)c);
             }
           System.out.println();
         }
      catch(IOException ioe)
         {
            System.out.println(ioe);
         }
      finally
         {
            if(is != null)
            {
               try
	    {
	        is.close();
	    }
               catch(Exception e)
	    {
	        System.out.print(e);
	    }
            }
          if(is1 != null)
            {
               try
	    {
	        is1.close();
	    }
               catch(Exception e1)
	    {
	        System.out.print(e1);
	    }
            }   
        }// end finally
   }// end main
}// end class

Output :

When you will execute the above code successfully you will get the output as follows :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 7, 2012

Related Tutorials

Discuss: Java IO SequenceInputStream Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Pooja Tekwani
June 19, 2013
SequenceInputStream

What is actually SequenceInputStream???
DMCA.com