黑马程序员技术交流社区

标题: enumercation只能由Vector获得吗? [打印本页]

作者: 清风有意    时间: 2014-4-19 19:48
标题: enumercation只能由Vector获得吗?
enumercation还可以由其他集合获取吗?不是说Vector已经淘汰了,被ArrayList替代了吗?那ArrayList可以获取enumercation吗?
代码:
  1. package IOTest;

  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.SequenceInputStream;
  7. import java.util.Vector;

  8. public class SequenceTest {

  9.         public static void main(String[] args) {
  10.                 FileOutputStream fos=null;
  11.                 SequenceInputStream sis=null;
  12.                 Vector<FileInputStream> vector=new Vector<FileInputStream>();
  13.                 try {
  14.                         fos=new FileOutputStream("4.txt");
  15.                         vector.add(new FileInputStream("2.txt"));
  16.                         vector.add(new FileInputStream("1.txt"));
  17.                         vector.add(new FileInputStream("3.txt"));
  18.                         sis=new SequenceInputStream(vector.elements());
  19.                         byte[] buf=new byte[1024];
  20.                         int len=0;
  21.                         while((len=sis.read(buf))!=-1)
  22.                         {
  23.                                 fos.write(buf, 0, len);
  24.                                
  25.                         }
  26.                 } catch (FileNotFoundException e) {
  27.                         // TODO Auto-generated catch block
  28.                         e.printStackTrace();
  29.                 } catch (IOException e) {
  30.                         // TODO Auto-generated catch block
  31.                         e.printStackTrace();
  32.                 }
  33.                 finally{
  34.                         try {
  35.                                 if(fos!=null)
  36.                                 fos.close();
  37.                         } catch (IOException e) {
  38.                                 // TODO Auto-generated catch block
  39.                                 e.printStackTrace();
  40.                         }finally{
  41.                                 try {
  42.                                         if(sis!=null)
  43.                                         sis.close();
  44.                                 } catch (IOException e) {
  45.                                         // TODO Auto-generated catch block
  46.                                         e.printStackTrace();
  47.                                 }
  48.                         }
  49.                        
  50.                 }
  51.                
  52.         }

  53. }
复制代码

作者: 曹冬明    时间: 2014-4-19 19:51
毕老师的视频里好像说过,这哥们唯一存在的理由就是能获取枚举
作者: 清风有意    时间: 2014-4-19 20:11
曹冬明 发表于 2014-4-19 19:51
毕老师的视频里好像说过,这哥们唯一存在的理由就是能获取枚举

哦,我想起来了!
作者: ノtrack    时间: 2014-4-19 22:09
  1. /*
  2. 切割文件


  3. 合并文件

  4. SequenceInputStream
  5. */
  6. import java.io.*;
  7. import java.util.*;
  8. class Demo8
  9. {
  10.         public static void main(String[] args)throws Exception
  11.         {                                                                                                         
  12.                 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("Dj.mp3"));
  13.                 BufferedOutputStream bos=null;
  14.                 byte []buf=new byte[1024*1024*2];
  15.                 int len=0;
  16.                 int count=0;
  17.                 while((len=bis.read(buf))!=-1){
  18.                         bos=new BufferedOutputStream(new FileOutputStream("split/Dj_"+(++count)+".part"));
  19.                         bos.write(buf,0,len);
  20.                         bos.close();
  21.                 }
  22.                 ArrayList<InputStream> al=new ArrayList<InputStream>();
  23.                 count=0;
  24.                 while(count!=3){
  25.                         al.add(new FileInputStream("split/Dj_"+(++count)+".part"));
  26.                 }
  27.                 final Iterator<InputStream> it=al.iterator();
  28.                 Enumeration<InputStream> en=new Enumeration<InputStream>(){
  29.                   public boolean hasMoreElements(){
  30.                         return it.hasNext();
  31.                   }
  32.                   public InputStream nextElement(){
  33.                         return it.next();
  34.                   }
  35.                 };
  36.                
  37.                 SequenceInputStream sis=new SequenceInputStream(en);
  38.                 PrintStream ps=new PrintStream("split/Dj.mp3");
  39.                 byte []b=new byte[1024];
  40.                 len=0;
  41.                 while((len=sis.read(b))!=-1){
  42.                    ps.write(b,0,len);
  43.                 }
  44.                 if(bis!=null)
  45.                         bis.close();
  46.                 if(bos!=null)
  47.                         bos.close();
  48.                 sis.close();
  49.                 ps.close();
  50.         }
  51. }
复制代码

作者: idream    时间: 2014-4-20 21:24
ArrayList不可以获取枚举,枚举是Vector特有的一种迭代方式




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2