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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 草上飞 中级黑马   /  2012-11-4 17:38  /  1186 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
import java.util.*;
class Spiltfiledemo
{
public static void main(String[] args) throws IOException
{
  //spiltfile();
  merge();
}
//用合并流合并数据
public static void merge() throws IOException
{
  ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
  for (int x=1;x<=3 ;x++ )
  {
   al.add(new FileInputStream("d:\\spilt\\"+x+".part"));//将所有切割的碎片添加到数组中
  }
  final Iterator<FileInputStream> it=al.iterator();
  Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
  {
    public boolean hasMoreElements()
       {
     return it.hasNext();//判断是否还有下一个元素   
    }
    public FileInputStream nextElement()
       {   
    return it.next();//如歌还有这放回下一个元素
    }
  };
  SequenceInputStream sis=new SequenceInputStream(en);//读取所有集合中的文件,
  FileOutputStream fos=new FileOutputStream("d:\\spilt\\1.bmp");//目的文件
  byte[] b=new byte[1024*10];
  int len=0;
  while ((len=sis.read(b))!=-1)
  {
   fos.write(b,0,len);   
  }
  fos.close();
  sis.close();
  

}
public static void spiltfile()throws IOException
{
  FileInputStream fis=new FileInputStream("d:\\2.jpg");
  FileOutputStream fos=null;
  byte[] b=new byte[1024*300];//第一个字节缓冲区
  int len=0;
  int count=1;
  while ((len=fis.read(b))!=-1)//将数据读取到byte中
  {
   fos=new FileOutputStream("d:\\spilt\\"+(count++)+".part");//将切割的碎片出入到spilt文件中
   fos.write(b,0,len);
   fos.close();
  }
  fis.close();


}
}

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 很给力!

查看全部评分

2 个回复

正序浏览
许个夸奖了,技术556了,是我几十倍,我向你学习才对。
回复 使用道具 举报
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马