All for Joomla All for Webmasters

Java SequenceInputStream class

ava SequenceInputStream class is used to read data from multiple streams. It reads data of streams one by one.

Constructors of SequenceInputStream class

Constructor Description
1) SequenceInputStream(InputStream s1, InputStream s2) creates a new input stream by reading the data of two input stream in order, first s1 and then s2.
2) SequenceInputStream(Enumeration e) creates a new input stream by reading the data of an enumeration whose type is InputStream.

Simple example of SequenceInputStream class

In this example, we are printing the data of two files f1.txt and f2.txt.

  1. import java.io.*;
  2. class Simple{
  3.   public static void main(String args[])throws Exception{
  4.    FileInputStream fin1=new FileInputStream(“f1.txt”);
  5.    FileInputStream fin2=new FileInputStream(“f2.txt”);
  6.    SequenceInputStream sis=new SequenceInputStream(fin1,fin2);
  7.    int i;
  8.    while((i=sis.read())!=-1){
  9.     System.out.println((char)i);
  10.    }
  11.    sis.close();
  12.    fin1.close();
  13.    fin2.close();
  14.   }
  15. }

Example that reads the data from two files and writes into another file

In this example, we are writing the data of two files f1.txt and f2.txt into another file named f3.txt.

  1. //reading data of 2 files and writing it into one file
  2. import java.io.*;
  3. class Simple{
  4.   public static void main(String args[])throws Exception{
  5.    FileInputStream fin1=new FileInputStream(“f1.txt”);
  6.    FileInputStream fin2=new FileInputStream(“f2.txt”);
  7.    FileOutputStream fout=new FileOutputStream(“f3.txt”);
  8.    SequenceInputStream sis=new SequenceInputStream(fin1,fin2);
  9.    int i;
  10.    while((i.sisread())!=-1)
  11.    {
  12.      fout.write(i);
  13.    }
  14.    sis.close();
  15.    fout.close();
  16.    fin1.close();
  17.    fin2.close();
  18.   }
  19. }

Example of SequenceInputStream class that reads the data from multiple files using enumeration

If we need to read the data from more than two files, we need to have these information in the Enumeration object. Enumeration object can be get by calling elements method of the Vector class. Let’s see the simple example where we are reading the data from the 4 files.
  1. import java.io.*;
  2. import java.util.*;
  3. class B{
  4. public static void main(String args[])throws IOException{
  5. //creating the FileInputStream objects for all the files
  6. FileInputStream fin=new FileInputStream(“A.java”);
  7. FileInputStream fin2=new FileInputStream(“abc2.txt”);
  8. FileInputStream fin3=new FileInputStream(“abc.txt”);
  9. FileInputStream fin4=new FileInputStream(“B.java”);
  10. //creating Vector object to all the stream
  11. Vector v=new Vector();
  12. v.add(fin);
  13. v.add(fin2);
  14. v.add(fin3);
  15. v.add(fin4);
  16. //creating enumeration object by calling the elements method
  17. Enumeration e=v.elements();
  18. //passing the enumeration object in the constructor
  19. SequenceInputStream bin=new SequenceInputStream(e);
  20. int i=0;
  21. while((i=bin.read())!=-1){
  22. System.out.print((char)i);
  23. }
  24. bin.close();
  25. fin.close();
  26. fin2.close();
  27. }
  28. }
PinIt
submit to reddit

Leave a Reply

Top