黑马程序员技术交流社区

标题: 合并流的中的Enumeration问题 [打印本页]

作者: 郑元皓    时间: 2013-4-27 21:27
标题: 合并流的中的Enumeration问题
本帖最后由 郑元皓 于 2013-5-1 14:02 编辑

package day20;
import java.io.*;
import java.util.*;
public class SplitFile {

        /**
         * @param args
         */
        public static void main(String[] args)throws IOException {
                // TODO Auto-generated method stub
                //splitFile();
                merge();
        }
        public static void merge()throws IOException
        {
                ArrayList<FileInputStream> a1=new ArrayList<FileInputStream>();
                for(int x=1;x<=5;x++)
                {
                        a1.add(new FileInputStream("D:\\JAVA123\\myproject\\acc\\"+x+".cpai"));
                }
                final Iterator<FileInputStream>it=a1.iterator();
                Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
                                {
                                public boolean hasMoreElenents()
                                {
                                        return it.hasNext();
                                }
                                public FileInputStream nextElement()
                                {
                                        return it.next();
                                }
                                };
                                SequenceInputStream sis=new SequenceInputStream(en);
                                
                                FileOutputStream fos=new FileOutputStream("D:\\JAVA123\\myproject\\acc\\小耗子1.jpg");
                                byte[] buf =new byte[1024];
                                int len=0;
                                while((len=sis.read(buf))!=-1)
                                {
                                        fos.write(buf, 0, len);
                                }
                                fos.close();
                                sis.close();
        }
        public static void splitFile()throws IOException
        {
                FileInputStream fis =new FileInputStream("D:\\JAVA123\\myproject\\acc\\小耗子.jpg");
                FileOutputStream fos=null;
                byte[] buf=new byte[1024*10];
                int len=0;
                int count=1;
                while((len=fis.read(buf))!=-1)
                {
                        fos=new FileOutputStream("D:\\JAVA123\\myproject\\acc\\"+(count++)+".cpai");
                        fos.write(buf,0,len);
                        fos.close();
                }
                fis.close();
               
               
                                
        }
}

为什么Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()这里一直报错啊。好郁闷的
作者: harborbest    时间: 2013-4-28 11:34
Enumeration是一个接口,你要想使用一个接口必须实现接口里边的方法。那么你就可以在这个Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()后写一个匿名内部类去实现 hasMoreElements() , nextElement()这俩个方法。





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