黑马程序员技术交流社区

标题: IO文件切割 可指定切割文件的大小 [打印本页]

作者: fmi110    时间: 2015-8-8 14:38
标题: IO文件切割 可指定切割文件的大小
IO
  1. /*
  2. 需求:切割一个文件

  3. 分析:        1、将要分割的文件封装至流
  4.                 2、目标文件封装至流
  5. */
  6. import java.io.*;
  7. import java.util.*;

  8. class SplitFile
  9. {
  10.         public static void main(String[] args) throws IOException
  11.         {
  12.                 split();//切割的文件大小为3M
  13. //                merge();//合并文件
  14.                 System.out.println("Hello World!");
  15.         }

  16.         public static void split() throws IOException  //SIZE设置切割后每部分文件的大小
  17.         {
  18.                 //切割文件
  19.                 FileInputStream fin = new FileInputStream("d:\\迅雷绿色破解版.rar");
  20.                
  21.                 //切割成3个文件,定义输出流,轮流指向三个输出流
  22.                 FileOutputStream fos = null;

  23.                 //创建1M缓冲区
  24.                 byte[] buf = new byte[1024*1024];
  25.                 int len = 0;//int是32位,支持不了大于4G的内容
  26.                 int SIZE = 5; //设置切割后每部分文件的大小
  27.                 int count = 1;//用于命名生成文件的序号
  28.                 int time = 0;
  29.                 while((len=fin.read(buf))!=-1){
  30.                         System.out.println("Cached... "+(float)len/(1024*1024)+"M ....");//在屏幕上输出提示缓冲的大小
  31.                         if(time==0)
  32.                                 fos = new FileOutputStream("d:\\"+(count)+".part");
  33.                         time++;//判断后加1
  34.                         fos.write(buf,0,len);  //单次写入文件
  35.                         fos.flush();
  36.                         System.out.println("flush...");
  37.                         if(time>=SIZE){ //控制分割的文件的大小为 SIZE
  38.                                 System.out.println("******************* "+(count)+" part finished!!******************");
  39.                                 count++;
  40.                                 time = 0;
  41.                         }       
  42.                 }
  43.                 fos.close();
  44.                 fin.close();
  45.         }
  46.         public static void merge() throws IOException
  47.         {
  48.                 //集合接受流
  49.                 ArrayList<FileInputStream> ar = new ArrayList<FileInputStream>();
  50.                 //添加要合并的文件
  51.                 ar.add(new FileInputStream("d:\\1.part"));
  52.                 ar.add(new FileInputStream("d:\\2.part"));
  53.                 //创建输出流
  54.                 FileOutputStream fos = null;

  55.                 final Iterator<FileInputStream> it = ar.iterator();//为保证传递给内部类后数据的一致性,用final修饰

  56.                 //复写枚举,并传递给SequenceInputStream
  57.                 Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
  58.                         public boolean hasMoreElements(){
  59.                                 return it.hasNext();
  60.                         }
  61.                         public FileInputStream nextElement(){
  62.                                 return it.next();
  63.                         }
  64.                 };

  65.                 SequenceInputStream sis = new SequenceInputStream(en);
  66.                 //缓冲
  67.                 byte[] buf = new byte[1024*1024];
  68.                 int len;
  69.                 fos = new FileOutputStream("d:\\mergeFile.jpg");
  70.                 while((len=sis.read(buf))!=-1){
  71.                         fos.write(buf,0,len);
  72.                         fos.flush();
  73.                 }
  74.                 fos.close();
  75.                 sis.close();
  76.         }
  77. }
复制代码



作者: fmi110    时间: 2015-8-8 14:44
自己顶一次  可以进入第21天的基础视频了




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