importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FilenameFilter;importjava.io.SequenceInputStream;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Enumeration;importjava.util.List;importjava.util.Properties;/*** SplitFile.java
*@author:bylv
* @DATE:2015-9-7 上午09:27:43
* Copyright (C) 2011 ISoftStone*/
/*** 功能说明:
*
*@author: bylv
* @DATE:2015-9-7 @TIME: 上午09:27:43*/
public classSplitFile {private static final int SIZE = 1024 * 1024;//定义单个文件的大小这里采用1m
/*** 功能说明:
*
*@paramargs
*@author: bylv
*@throwsException
* @DATE:2015-9-7 @TIME: 上午09:27:44*/
public static void main(String[] args) throwsException {//拆分//File file = new File("D:/无标题.bmp");//splitFile(file);//合并
File file = new File("C:/parfiles");
mergeFile(file);
}/*** 功能说明:合并文件
*
*@paramfile
*@author: bylv
*@throwsException
*@throwsFileNotFoundException
* @DATE:2015-9-7 @TIME: 上午09:47:31*/
private static void mergeFile(File dir) throwsException {//读取properties文件的拆分信息
File[] files = dir.listFiles(newFilenameFilter() {
@Overridepublic booleanaccept(File dir, String name) {return name.endsWith(".properties");
}
});
File file= files[0];//获取该文件的信息
Properties pro = newProperties();
FileInputStream fis= newFileInputStream(file);
pro.load(fis);
String fileName= pro.getProperty("fileName");int splitCount = Integer.valueOf(pro.getProperty("partCount"));if (files.length != 1) {throw new Exception(dir + ",该目录下没有解析的properties文件或不唯一");
}//获取该目录下所有的碎片文件
File[] partFiles = dir.listFiles(newFilenameFilter() {
@Overridepublic booleanaccept(File dir, String name) {return name.endsWith(".part");
}
});//将碎片文件存入到集合中
List al = new ArrayList();for (int i = 0; i < splitCount; i++) {try{
al.add(newFileInputStream(partFiles[i]));
}catch(Exception e) {//异常
e.printStackTrace();
}
}try{//构建文件流集合
Enumeration en =Collections.enumeration(al);//将多个流合成序列流
SequenceInputStream sis = newSequenceInputStream(en);
FileOutputStream fos= new FileOutputStream(newFile(dir, fileName));byte[] b = new byte[1024];int len = 0;while ((len = sis.read(b)) != -1) {
fos.write(b,0, len);
}
fos.close();
sis.close();
}catch(Exception e) {
e.printStackTrace();
}
}/*** 功能说明:拆分文件
*
*@paramfile
*@author: bylv
* @DATE:2015-9-7 @TIME: 上午09:28:58*/
private static voidsplitFile(File file) {try{
FileInputStream fs= newFileInputStream(file);//定义缓冲区
byte[] b = new byte[SIZE];
FileOutputStream fo= null;int len = 0;int count = 0;/*** 切割文件时,记录 切割文件的名称和切割的子文件个数以方便合并
* 这个信息为了简单描述,使用键值对的方式,用到了properties对象*/Properties pro= newProperties();//定义输出的文件夹路径
File dir = new File("C:/parfiles");//判断文件夹是否存在,不存在则创建
if (!dir.exists()) {
dir.mkdirs();
}//切割文件
while ((len = fs.read(b)) != -1) {
fo= new FileOutputStream(new File(dir, (count++) + ".part"));
fo.write(b,0, len);
fo.close();
}//将被切割的文件信息保存到properties中
pro.setProperty("partCount", count + "");
pro.setProperty("fileName", file.getName());
fo= new FileOutputStream(new File(dir, (count++) + ".properties"));//写入properties文件
pro.store(fo, "save file info");
fo.close();
fs.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}