AdSense

AdSense3

Tuesday 4 November 2014

Java SequenceInputStream class

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

Constructors of SequenceInputStream class:

ConstructorDescription
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.   
  7.    SequenceinputStream sis=new SequenceinputStream(fin1,fin2);  
  8.    int i;  
  9.    while((i=sis.read())!=-1){  
  10.     System.out.println((char)i);  
  11.    }  
  12.    sis.close();  
  13.    fin1.close();  
  14.    fin2.close();  
  15.   }  
  16. }  

Example of SequenceInputStream that reads the data from two files

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.   
  3. import java.io.*;  
  4. class Simple{  
  5.   public static void main(String args[])throws Exception{  
  6.   
  7.    FileinputStream fin1=new FileinputStream("f1.txt");  
  8.    FileinputStream fin2=new FileinputStream("f2.txt");  
  9.   
  10.    FileOutputStream fout=new FileOutputStream("f3.txt");  
  11.   
  12.    SequenceinputStream sis=new SequenceinputStream(fin1,fin2);  
  13.    int i;  
  14.    while((i.sisread())!=-1)  
  15.    {  
  16.      fout.write(i);      
  17.    }  
  18.    sis.close();  
  19.    fout.close();    
  20.    fin.close();    
  21.    fin.close();    
  22.   
  23.   }  
  24. }  

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.   
  4. class B{  
  5. public static void main(String args[])throws IOException{  
  6.   
  7. //creating the FileInputStream objects for all the files  
  8. FileInputStream fin=new FileInputStream("A.java");  
  9. FileInputStream fin2=new FileInputStream("abc2.txt");  
  10. FileInputStream fin3=new FileInputStream("abc.txt");  
  11. FileInputStream fin4=new FileInputStream("B.java");  
  12.   
  13. //creating Vector object to all the stream  
  14. Vector v=new Vector();  
  15. v.add(fin);  
  16. v.add(fin2);  
  17. v.add(fin3);  
  18. v.add(fin4);  
  19.   
  20. //creating enumeration object by calling the elements method  
  21. Enumeration e=v.elements();  
  22.   
  23. //passing the enumeration object in the constructor  
  24. SequenceInputStream bin=new SequenceInputStream(e);  
  25. int i=0;  
  26.   
  27. while((i=bin.read())!=-1){  
  28. System.out.print((char)i);  
  29. }  
  30.   
  31. bin.close();  
  32. fin.close();  
  33. fin2.close();  
  34. }  
  35. }

No comments:

Post a Comment