SplitFileDemo(分割)类
package Split;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class SplitFileDemo {
private static final int PART_SIZE = 1048576;
/**
* 将文件切成1m大小文件碎片。
*
*
*/
public static void main(String[] args) throws IOException {
File file = new File("F:\\等一分钟.avi");
File dir = new File("F:\\splitPart1");
splitFile(file,dir);
}
public static void splitFile(File file, File dir) throws IOException {
FileInputStream fis = new FileInputStream(file);
if(!dir.exists())
dir.mkdirs();
FileOutputStream fos = null;
byte []buf = new byte[PART_SIZE];
int len = 0;
int count = 1;
while((len = fis.read(buf)) != -1){
fos = new FileOutputStream(new File(dir,(count++)+".myPart"));
fos.write(buf, 0, len);
fos.close();
}
FileWriter fw = new FileWriter(new File(dir,"fileInfo.properties"));
Properties pro = new Properties();
pro.setProperty("partCount", count+"");
pro.setProperty("fileName", file.getName());
pro.store(fw, "fileInfo");
fw.close();
fis.close();
}
}
=====================================================================================================================================
mergeFileDemo(合并类)
package merge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
public class mergeFileDemo {
/**
* 文件合并程序。
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File dir = new File("F:\\splitPart1");
mergeFile(dir);
}
public static void mergeFile(File dir) throws IOException {
File[] properties_file = dir.listFiles(new Suffix("properties"));
if(properties_file.length != 1)
throw new RuntimeException("配置文件不存在 或 不唯一");
Properties pro = new Properties();
pro.load(new FileReader(properties_file[0]));
int count = Integer.parseInt(pro.getProperty("partCount"));
String file_name = pro.getProperty("fileName");
File[] myPart = dir.listFiles(new Suffix(".myPart"));
if(myPart.length != count-1)
throw new RuntimeException("碎片文件数目不正确! 应为 "+(count-1)+" 个");
ArrayList<InputStream> al = new ArrayList<InputStream>();
FileInputStream fis = null;
for(int i = 1; i<count ;i++){
fis = new FileInputStream(new File(dir,i+".myPart"));
al.add(fis);
}
//创建枚举变量 来存取集合中的读入流。
Enumeration<InputStream> en = Collections.enumeration(al);
//将枚举变量中的读取流,进行逻辑串联。
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir,file_name));
byte []buf = new byte[1024];
int len = 0;
while((len = sis.read(buf))!= -1){
fos.write(buf, 0, len);
fos.flush();
}
fos.close();
sis.close();
}
}
===========================================================================================================================================================================
过滤器类
package merge;
import java.io.File;
import java.io.FilenameFilter;
public class Suffix implements FilenameFilter {
private String suffix;
public Suffix(String suffix) {
super();
this.suffix = suffix;
}
public boolean accept(File dir, String name) {
return name.endsWith(suffix);
}
}