合并碎片文件

package com.java.merge.www;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import javax.management.RuntimeErrorException;

public class MergeDmeo {

 public static void main(String[] args) throws IOException {
  //创建一个字节输入流
  File file=new File("D:\ja\spart");
  //获取配置文件
  File fileConfig=getFileConfig(file);
  //为获取配置文件中的配置信息,创建一个属性类
  Properties prop=getpProperties(fileConfig);
  //等到配置文件中要使用的信息
  Integer count= new Integer(prop.getProperty("i"));
  String filename=prop.getProperty("filename");
  //创建一个合并的方法
  Meger(file,count,filename);
 }
 /**
  * 获取配置文件信息
  * @param fileConfig
  * @throws IOException
  */
 private static Properties getpProperties(File fileConfig) throws IOException {
  //把配置文件写入写入流中
  FileInputStream fis=new FileInputStream(fileConfig);
  // 创建一个Properties 类
  Properties prop=new Properties();
  //加载这个写入流
  prop.load(fis);
  fis.close();
  return prop;
 }
 /**
  * 获取配置文件
  * @param file
  */
 
 private static File getFileConfig(File file) {
  //判断这个目录是否存在和是否是目录
  if(!(file.exists()&&file.isDirectory())){
   throw new RuntimeException("该文件不存在或者不是目录");
  }
  //过滤里面的文件们 来获取指定的配置文件
  File[] files=file.listFiles(new FileFilter() {
   
   @Override
   public boolean accept(File filenames) {
    
    return filenames.getName().endsWith(".properties");
   }
  });
  int len=files.length;
  if(len!=1){
   throw new RuntimeException("配置文件过多,无法完成以下操作");
  }
  return files[0];
 }
 /**
  * 合并碎片
  * @param file
  * @throws IOException
  */
 private static void Meger(File file,Integer count,String filename) throws FileNotFoundException, IOException {
  //创建个集合
  List<FileInputStream> list=new ArrayList<FileInputStream>();
  for(int i=1;i<count;i++){
   list.add(new FileInputStream(new File(file,i+".avi")));
  }
  //创建个枚举对象
  Enumeration<FileInputStream> en=Collections.enumeration(list);
  // 创建逻辑流
  SequenceInputStream sqis=new SequenceInputStream(en);
  //创建一个字节输出流
  FileOutputStream fos=new FileOutputStream(new File(file,filename));
  //创建个缓冲区
  byte[] buf=new byte[1024*2048];
  int ch=0;
  while((ch=sqis.read(buf))!=-1){
   fos.write(buf,0,ch);
  }
  sqis.close();
  fos.close();
 }
}

原文地址:https://www.cnblogs.com/daoxiang1992/p/5778051.html