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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奔跑的二叉树 中级黑马   /  2013-9-19 13:48  /  1085 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 奔跑的二叉树 于 2013-9-20 20:55 编辑
  1. package cn.baidu.com;

  2. import java.io.*;
  3. import java.util.*;
  4. //
  5. public class SplitFile {

  6.         public static void main(String[] args) throws IOException {
  7.                 splitFile();
  8.                 merge();
  9.         }
  10.         public static void merge() throws IOException
  11.         {
  12.                 ArrayList<FileInputStream> a1=new ArrayList<FileInputStream>();
  13.                
  14.                 for(int x=1;x<=3;x++)
  15.                 {
  16.                         a1.add(new FileInputStream("F:\\Demo"+x+".part"));
  17.                         
  18.                 }
  19.                 final Iterator<FileInputStream> it=a1.iterator();
  20.                
  21.                 Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
  22.                 {
  23.                         public boolean hasMoreElements()
  24.                         {
  25.                                 return it.hasNext();
  26.                         }
  27.                         public FileInputStream nextElement()
  28.                         {
  29.                                 return it.next();
  30.                         }
  31.                 };
  32.                
  33.                 SequenceInputStream sis=new SequenceInputStream(en);
  34.                 FileOutputStream fos=new FileOutputStream("F:\\NEW-DHTML.chm");
  35.                 byte[] buf=new byte[1024];
  36.                 int len=0;
  37.                 while((len=sis.read(buf))!=-1)
  38.                 {
  39.                         fos.write(buf, 0, len);
  40.                 }
  41.                
  42.                 fos.close();
  43.                 sis.close();
  44.         }
  45.         public static void splitFile() throws IOException
  46.         {
  47.                 FileInputStream fis=new FileInputStream("F:\\DHTML.chm");
  48.                 FileOutputStream fos=null;
  49.                 byte[] buf=new byte[1024*1024];
  50.                 int len=0;
  51.                 int count=1;
  52.                 while((len=fis.read(buf))!=-1)
  53.                 {
  54.                         fos=new FileOutputStream("F:\\Demo"+(count++)+".part");
  55.                         fos.write(buf, 0, len);
  56.                         fos.close();
  57.                 }
  58.                 fis.close();
  59.         }


  60. }
复制代码
把分割的文件合并成chm文件时,合并后的文件比之前的文件小了,而且打不开

评分

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

查看全部评分

1 个回复

倒序浏览
一个part文件中存的1M数据,如果dhtml.chm文件超过3M,而arraylist集合中做多只能存三个io流,不能保证全部的part文件都写入新文件,会出现新文件比源文件小的情况;

评分

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

查看全部评分

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