A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 清风有意 中级黑马   /  2014-4-19 19:48  /  1207 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

4 个回复

倒序浏览
毕老师的视频里好像说过,这哥们唯一存在的理由就是能获取枚举
回复 使用道具 举报
曹冬明 发表于 2014-4-19 19:51
毕老师的视频里好像说过,这哥们唯一存在的理由就是能获取枚举

哦,我想起来了!
回复 使用道具 举报
  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. }
复制代码
回复 使用道具 举报
ArrayList不可以获取枚举,枚举是Vector特有的一种迭代方式
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马