JAVA自学教程之IO流(九)文件切割合成器实例源码



JAVA自学教程之IO流(九)文件切割合成器实例源码。

文件切割器

 

  1. private static final int SIZE = 1024 *1024;
  2.     public static void splitFile(File file) throws IOException{
  3.         //用读取流关联文件(不确定文件格式)
  4.         FileInputStream fis = new FileInputStream(file);//源是一个
  5.         byte[] by = new byte[SIZE];//定义1M的缓冲区
  6.         FileOutputStream fos = null;//汇不知道有多少个
  7.         int len = 0;
  8.         int count = 1;//记录子文件个数
  9.         File dir = new File(“D:\\patFiles”);
  10.         if(!dir.isFile()){
  11.             dir.mkdirs();
  12.         }
  13.         while((len = fis.read(by))!=-1){
  14.             fos = new FileOutputStream(new File(dir,(count++)+”.part”));//自定义文件格式
  15.             fos.write(by,0,len);
  16.         }
  17.         fos.close();
  18.         fis.close();
  19.     }

 

 


文件合并

 

  1. public static void main(String[] args) throws IOException {
  2.         File file = new File(“D:\\PartFile”);
  3.         Merge(file);
  4.     }
  5.     public static void Merge(File dir)throws IOException{
  6.         ArrayList<FileInputStream> AL = new ArrayList<FileInputStream>();
  7.         for(int i = 1;i<=7;i++){
  8.             AL.add(new FileInputStream(new File(dir,i+”.part”)));
  9.         }
  10.         Enumeration<FileInputStream> en = Collections.enumeration(AL);
  11.         SequenceInputStream sis = new SequenceInputStream(en);
  12.         FileOutputStream fos = new FileOutputStream(new File(dir,”盛夏光年.mp3″));
  13.         byte[] by = new byte[1024];
  14.         int len = 0;
  15.         while((len = sis.read(by))!=-1){
  16.             fos.write(by, 0, len);
  17.         }
  18.         sis.close();
  19.         fos.close();
  20.     }

文件切割合并+配置文件

 

 

  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Enumeration;
  5. import java.util.Properties;
  6. public class Main
  7. {
  8.         private static final int SIZE = 1024 *1024;
  9.     public static void main(String[] args) throws IOException {
  10.         File file1 = new File(“d:\\NeedSplit\\盛夏光年.mp3″);
  11.         File file2 = new File(“D:\\PartFiles”);
  12.         splitFile(file1);
  13.         Merge_1(file2);
  14.     }
  15.     public static void splitFile(File file) throws IOException{
  16.         //用读取流关联文件(不确定文件格式)
  17.         FileInputStream fis = new FileInputStream(file);//源是一个
  18.         byte[] by = new byte[SIZE];//定义1M的缓冲区
  19.         FileOutputStream fos = null;//汇不知道有多少个
  20.         int len = 0;
  21.         int count = 1;//记录子文件个数
  22.         /*切割文件必须要记录切割文件的名称和切割处理的碎片文件的个数,方便合并
  23.          * 这个信息为了进行描述,使用键值对的方法,所以使用Properties对象*/
  24.         Properties pro = new Properties();
  25.         File dir = new File(“D:\\PartFiles”);
  26.         if(!dir.isFile()){
  27.             dir.mkdirs();
  28.         }
  29.         while((len = fis.read(by))!=-1){
  30.             fos = new FileOutputStream(new File(dir,(count++)+”.part”));//自定义文件格式
  31.             fos.write(by,0,len);
  32.             fos.close();
  33.         }
  34.         //将切割后文件的信息保存在pro集合中
  35.         pro.setProperty(“partCount”, count+”");
  36.         pro.setProperty(“fileName”, file.getName());
  37.         fos = new FileOutputStream(new File(dir,count+”.properties”));
  38.         //将pro集合的信息存储在集合中
  39.         pro.store(fos, ”save file infmation”);
  40.         fis.close();
  41.     }
  42.     public static void Merge_1(File dir)throws IOException{
  43.         //获取指定目录下配置文件对象
  44.         File[] files = dir.listFiles(new SuffixFilter(“.properties”));//new一个过滤器
  45.         if(files.length!=1){
  46.             throw new RuntimeException(dir+”该目录下没有properties扩展名的文件或者不唯一 ”);
  47.         }
  48.         //记录配置文件对象
  49.         File confile = files[0];
  50.         //获取配置文件信息
  51.         Properties pro = new Properties();
  52.         FileInputStream fis = new FileInputStream(confile);//关联流对象
  53.         pro.load(fis);//加载信息
  54.         String filename  = pro.getProperty(“fileName”);//得到文件名
  55.         int count = Integer.parseInt(pro.getProperty(“partCount”));//得到碎片个数
  56.         //获取该目录下的所有碎片文件
  57.         //定义过滤器,判断碎片文件的个数与配置信息中的碎片信息是否一致
  58.         File[] partFiles = dir.listFiles(new SuffixFilter(“.part”));
  59.         if(partFiles.length!=(count-1)){
  60.             throw new RuntimeException(“碎片文件个数不对,应是”+count+”个!”);
  61.         }
  62.         //将碎片文件和流对象关联,并存储集合中
  63.         ArrayList<FileInputStream> AL = new ArrayList<FileInputStream>();
  64.         for(int i = 0;i<partFiles.length;i++){
  65.             AL.add(new FileInputStream(partFiles[i]));
  66.         }
  67.         //将多个流合并成一个序列流
  68.         Enumeration<FileInputStream> en = Collections.enumeration(AL);
  69.         SequenceInputStream sis = new SequenceInputStream(en);
  70.         //读写过程
  71.         FileOutputStream fos = new FileOutputStream(new File(dir,filename));
  72.         byte[] by = new byte[1024];
  73.         int len = 0;
  74.         while((len = sis.read(by))!=-1){
  75.             fos.write(by, 0, len);
  76.         }
  77.         sis.close();
  78.         fos.close();
  79.     }
  80. }