将1.txt,2.txt,3.txt文件中的数据合并到一个文件中
实例1
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public static void demo_2() throws IOException { 2 // TODO Auto-generated method stub 3 ArrayList<FileInputStream> al=new ArrayList<FileInputStream>(); 4 for(int x=0;x<=3;x++){ 5 al.add(new FileInputStream(x+".txt")); 6 } 7 Enumeration<FileInputStream> 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();
实例2
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
ArrayList<FileInputStream> al=new ArrayList<FileInputStream>(); for(int x=0;x<=3;x++){ al.add(new FileInputStream(x+".txt")); } final Iterator<FileInputStream> it=al.iterator(); Enumeration<FileInputStream> en=new Enumeration<FileInputStream>(){ @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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Vector<FileInputStream> v=new Vector<FileInputStream>(); 2 v.add(new FileInputStream("1.txt")); 3 v.add(new FileInputStream("2.txt")); 4 v.add(new FileInputStream("3.txt")); 5 Enumeration<FileInputStream> 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();