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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangcongwu 中级黑马   /  2014-12-15 04:20  /  1003 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

RT,sequenceInputStream 的实验,合并流试图用Enumeration 照着老毕的视频敲 的代码不行啊,我网上找到一个行的,人家是try catch 和finally三步走的就行,老毕的throws IOException的不成啊。老毕坑我
  1. package 合并流SequenceInputStream;
  2. import java.io.*;
  3. import java.util.*;
  4. public class SequenceInputStream {

  5.         public static void main(String[] args) throws IOException {
  6.                
  7.                 // 只有vector 有emoration
  8.                 doSequence();
  9.              
  10.         }
  11.        
  12.         public static void  doSequence()
  13.         {
  14.                   SequenceInputStream sis = null;  
  15.               Vector<InputStream> v = new Vector<InputStream>();
  16.                 v.addElement(new FileInputStream("1.txt"));
  17.                 v.addElement(new FileInputStream("2.txt"));
  18.                 v.addElement(new FileInputStream("3.txt"));

  19.                 Enumeration<InputStream> en = v.elements();
  20.                  sis = new SequenceInputStream(en);               
  21.                 FileOutputStream fos = new FileOutputStream("4.txt");
  22.                 byte[] buf = new byte[1024];
  23.                 int len = 0;
  24.                 while((len = sis.read(buf))!= -1)
  25.                 {
  26.                         fos.write(buf,0,len);
  27.                 }
  28.                 fos.close();
  29.                 sis.close();
  30.         }
  31. }
复制代码

4 个回复

倒序浏览
神了人家另一个人用的是老毕的方法,我把他的放在我的文件里就不行了
  1. package 合并流SequenceInputStream;  
  2.       
  3.     import java.io.File;  
  4.     import java.io.FileInputStream;  
  5.     import java.io.FileOutputStream;  
  6.     import java.io.IOException;  
  7.     import java.io.InputStream;  
  8.     import java.io.SequenceInputStream;  
  9.     import java.util.ArrayList;  
  10.     import java.util.Collections;  
  11.     import java.util.Enumeration;  
  12.     import java.util.Iterator;  
  13.     import java.util.Vector;  
  14.       
  15.     public class 网上的用throws的方法写的 {  
  16.       
  17.         /**
  18.          * @param args
  19.          * @throws IOException  
  20.          */  
  21.         public static void main(String[] args) throws IOException {  
  22.               
  23.             /**
  24.              * 需求:将多个文件数据进行合并到一个文件中。
  25.              *  
  26.              * SequenceInputStream构造方法:
  27.              * SequenceInputStream sis = new SequenceInputStream(Enumeration<? extends InputStream> e);//枚举变量参数
  28.              */  
  29.           show1();  
  30.               
  31.     //      show2();  
  32.             //show3();  
  33.       
  34.         }  
  35.       
  36.         public static void show3() throws IOException {  
  37.             ArrayList<InputStream> al = new ArrayList<InputStream>();  
  38.               
  39.             for(int i = 1 ; i<4 ; i++){  
  40.                 al.add(new FileInputStream(i+".txt"));  
  41.             }  
  42.             Enumeration<InputStream> en = Collections.enumeration(al);   
  43.             SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
  44.               
  45.             FileOutputStream fos = new FileOutputStream(new File("4.txt"));  
  46.               
  47.             int len = 0;  
  48.             byte []bu = new byte[1024];  
  49.             while((len = sis.read(bu))!=-1){  
  50.                 fos.write(bu, 0, len);  
  51.             }  
  52.               
  53.             sis.close();  
  54.             fos.close();  
  55.         }  
  56.       
  57.         public static void show2() throws IOException {  
  58.             ArrayList<InputStream> al = new ArrayList<InputStream>();  
  59.             for(int i = 1 ; i<4 ; i++){  
  60.                 al.add(new FileInputStream(i+".txt"));  
  61.             }  
  62.             final Iterator<InputStream> it = al.iterator(); //内部类访问外部类中的成员变量,需要用final 修饰。  
  63.             Enumeration<InputStream> en = new Enumeration<InputStream>() {  
  64.       
  65.                 @Override  
  66.                 public boolean hasMoreElements() {  
  67.                      
  68.                     return it.hasNext();  
  69.                 }  
  70.       
  71.                 @Override  
  72.                 public InputStream nextElement() {  
  73.                      
  74.                     return it.next();  
  75.                 }            
  76.             };  
  77.               
  78.             SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
  79.               
  80.             FileOutputStream fos = new FileOutputStream(new File("4.txt"));  
  81.               
  82.             int len = 0;  
  83.             byte []bu = new byte[1024];  
  84.             while((len = sis.read(bu))!=-1){  
  85.                 fos.write(bu, 0, len);  
  86.             }  
  87.               
  88.             sis.close();  
  89.             fos.close();  
  90.         }  
  91.       
  92.         public static void show1() throws IOException {  
  93.             Vector<InputStream> v = new Vector<InputStream>();  
  94.             v.add(new FileInputStream("1.txt"));  
  95.             v.add(new FileInputStream("2.txt"));  
  96.             v.add(new FileInputStream("3.txt"));  
  97.             Enumeration<InputStream> en = v.elements();//获得枚举变量  
  98.             SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
  99.               
  100.             FileOutputStream fos = new FileOutputStream(new File("4.txt"));  
  101.               
  102.             int len = 0;  
  103.             byte []bu = new byte[1024];  
  104.             while((len = sis.read(bu))!=-1){  
  105.                 fos.write(bu, 0, len);  
  106.             }     
  107.             sis.close();  
  108.             fos.close();  
  109.         }  
  110.     }
复制代码
回复 使用道具 举报
上边的show1 就是老毕的方法,我放进去就是不行给我显示的是
        SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
说的是
The constructor SequenceInputStream(Enumeration<InputStream>) is undefined

啥几吧意思啊咋没defined啊
回复 使用道具 举报
我知道啥意思了 我2了
回复 使用道具 举报
评论一下,先收藏了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马