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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郑元皓 中级黑马   /  2013-4-27 21:27  /  1176 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 郑元皓 于 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>()这里一直报错啊。好郁闷的

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

1 个回复

倒序浏览
Enumeration是一个接口,你要想使用一个接口必须实现接口里边的方法。那么你就可以在这个Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()后写一个匿名内部类去实现 hasMoreElements() , nextElement()这俩个方法。

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马