利用Base64编码,再截字符串,仅支持小文件
小文件文件名随机,所以要将大文件信息和小文件顺序写入到小文件的第一行
commons-codec
commons-codec
1.6
importorg.apache.commons.codec.binary.Base64;import java.io.*;importjava.util.HashMap;importjava.util.Map;/*** Created by Liwj on 2016/8/23.*/
public classFileSplit {/*** 分割
*@paramfileName 文件路径
*@paramNumber 分块文件个数
*@throwsException*/
public void splitByNumber(String fileName,int Number) throwsException{
File oldFile=newFile(fileName);
BufferedInputStream in=new BufferedInputStream(newFileInputStream(oldFile));
String file=encode(in);int length=file.length();
System.out.println("字符串长度:"+length);int size=length/Number;int start=0,end=size;
BufferedOutputStream out=null;
File newFile=null;
String str_temp=null;for(int i=0;i
str_temp=i+" "+oldFile.getName()+"\n";
str_temp+=file.substring(start,end);
newFile=new File("E:\\result\\"+randNumber()+".file");
out=new BufferedOutputStream(newFileOutputStream(newFile));
out.write(str_temp.getBytes());
out.close();
start+=size;
end+=size;
}
str_temp=Number-1+" "+oldFile.getName()+"\n";
str_tem