将1.txt,2.txt,3.txt文件中的数据合并到一个文件中
实例1
1 public static void demo_2() throws IOException { 2 // TODO Auto-generated method stub 3 ArrayListal=new ArrayList (); 4 for(int x=0;x<=3;x++){ 5 al.add(new FileInputStream(x+".txt")); 6 } 7 Enumeration en=Collections.enumeration(al); 8 SequenceInputStream sis=new SequenceInputStream(en); 9 // BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("4.txt"));10 FileOutputStream fos=new FileOutputStream("4.txt");11 byte[] buf=new byte[1024];12 int len=0;13 while((len=sis.read(buf))!=-1){14 fos.write(buf, 0, len);15 }16 17 fos.close();18 sis.close();
ArrayListal=new ArrayList (); for(int x=0;x<=3;x++){ al.add(new FileInputStream(x+".txt")); } final Iterator it=al.iterator(); Enumeration en=new Enumeration (){ @Override public boolean hasMoreElements() { // TODO Auto-generated method stub return it.hasNext(); } @Override public FileInputStream nextElement() { // TODO Auto-generated method stub return it.next(); } };
实例3
1 Vectorv=new Vector (); 2 v.add(new FileInputStream("1.txt")); 3 v.add(new FileInputStream("2.txt")); 4 v.add(new FileInputStream("3.txt")); 5 Enumeration en=v.elements(); 6 SequenceInputStream sis=new SequenceInputStream(en); 7 // BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("4.txt")); 8 FileOutputStream fos=new FileOutputStream("4.txt"); 9 byte[] buf=new byte[1024];10 int len=0;11 while((len=sis.read(buf))!=-1){12 fos.write(buf, 0, len);13 }14 15 fos.close();16 sis.close();