IO streams Java learning (sequence flow --SequenceInputStream)

SequenceInputStream represents the logical series of other input streams.
It starts reading from an ordered set of input stream starts from the first, until it reaches the end of the file, and then read from the second, and so on, until it reaches the end of the input stream file included in the final

Constructor

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

Example:

1      // The combined 1.txt, 2.txt, 3.txt file into a file 
2      / * 
3       * a manner, Vector collection is feasible, but inefficient
 . 4       * / 
. 5      / * the Vector <the FileInputStream> = Vector new new the Vector <the FileInputStream> ();
 . 6      vector.add ( new new the FileInputStream ( "1.txt" ));
 . 7      vector.add ( new new the FileInputStream ( "2.txt" ));
 . 8      vector.add ( new new the FileInputStream ( ". 3 .txt " ));
 . 9      the Enumeration <the FileInputStream> EN = vector.elements ();
 10      A SequenceInputStream SS = new newA SequenceInputStream (EN); * /
 . 11      
12 is      / * 
13 is       * Second way, a high collection efficiency ArrayList
 14       * using the method of collection tools Collections.enumeration ArrayList set turn into Enumeration <FileInputStream> Object type
 15       * / 
16      
. 17      ArrayList <the FileInputStream > ARR = new new the ArrayList <the FileInputStream> ();
 18 is      
. 19      arr.add ( new new the FileInputStream ( "1.txt" ));
 20 is      arr.add ( new new the FileInputStream ( "2.txt" ));
 21 is      arr.add ( new new the FileInputStream ( "3.txt" ));
 22 is      A SequenceInputStream SS1 =new new SequenceInputStream (Collections.enumeration (arr));
 23      / * 
24-       * Collections.enumeration (arr) the realization of the principle:
 25       * 
 26       * Enumeration API note: at The functions on this page of the this interface IS Duplicated by at The Iterator interface
 27       * Translation: The the interface function is copied Iterator interface
 28       * 
 29       * Final Iterator <the FileInputStream> IT = arr.iterator ();
 30       * a SequenceInputStream SS1 new new = a SequenceInputStream (the Enumeration new new <the InputStream> () {
 31 is                  @Override
 32                  public Boolean the hasMoreElements () {
 33 is                  
34 is                 return it.hasNext();
35                 }
36         
37                 @Override
38                 public InputStream nextElement() {
39                 
40                 return it.next();
41                 }
42         });
43      */
44     byte[] buff=new byte[1024];
45     FileOutputStream out=new FileOutputStream("4.txt");
46     int len=0;
47     while((len=ss1.read(buff))!=-1) {
48         out.write(buff,0,len);
49     }
50     out.close();
51     
52     ss1.close();
53     

CUTTING file:

. 1      public  static  void SplitFile (File File) throws IOException {
 2      // with associated read stream file 
. 3      the FileInputStream FIS = new new the FileInputStream (File);
 . 4  
. 5      // define a buffer of 1M 
. 6      byte [] = buf new new  byte [ SIZE];
 . 7  
. 8      // Create object 
. 9      a FileOutputStream fos = null ;
 10  
. 11      int len = 0 ;
 12 is      int COUNT =. 1 ;
 13 is      File the dir = new new File("partfiles");
14 
15     if (!dir.exists()) {
16         dir.mkdirs();
17     }
18 
19     while ((len = fis.read(buf)) != -1) {
20         fos = new FileOutputStream(new File(dir, (count++) + ".part"));
21         fos.write(buf,0,len);
22     }
23     fos.close();
24     fis.close();
25     }

 

 

Guess you like

Origin www.cnblogs.com/WarBlog/p/12141611.html