Java 文件分块及合并
利用Base64编码,再截字符串,仅支持小文件
小文件文件名随机,所以要将大文件信息和小文件顺序写入到小文件的第一行
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.6</version> </dependency>
import org.apache.commons.codec.binary.Base64; import java.io.*; import java.util.HashMap; import java.util.Map; /** * Created by Liwj on 2016/8/23. */ public class FileSplit { /** * 分割 * @param fileName 文件路径 * @param Number 分块文件个数 * @throws Exception */ public void splitByNumber(String fileName,int Number) throws Exception{ File oldFile=new File(fileName); BufferedInputStream in=new BufferedInputStream(new FileInputStream(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<Number-1;i++){ str_temp=i+" "+oldFile.getName()+"\n"; str_temp+=file.substring(start,end); newFile=new File("E:\\result\\"+randNumber()+".file"); out=new BufferedOutputStream(new FileOutputStream(newFile)); out.write(str_temp.getBytes()); out.close(); start+=size; end+=size; } str_temp=Number-1+" "+oldFile.getName()+"\n"; str_temp+=file.substring(start); newFile=new File("E:\\result\\"+randNumber()+".file"); out=new BufferedOutputStream(new FileOutputStream(newFile)); out.write(str_temp.getBytes()); out.close(); return; } /** * 文件合并 * @param path * @throws Exception */ public void mergeByName(String path) throws Exception{ File file=new File(path); File list[]=file.listFiles(); Map<String,String> map=new HashMap<String, String>(); String newFileName=null; for(File f:list){ BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f))); String str_head=reader.readLine(); String id=str_head.substring(0,str_head.indexOf(" ")); newFileName=str_head.substring(str_head.indexOf(" ")+1); map.put(id,f.getAbsolutePath()); reader.close(); } StringBuffer stringBuffer=new StringBuffer(); for(int i=0;i<list.length;i++){ File f=new File(map.get(String.valueOf(i))); BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f))); reader.readLine(); String temp=null; while ((temp=reader.readLine())!=null){ stringBuffer.append(temp); } reader.close(); } BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("E:\\answer\\"+newFileName)); out.write(decode(stringBuffer.toString())); out.close(); } /** * 编码 * @param in * @return * @throws IOException */ public String encode(InputStream in) throws IOException{ byte[] data = new byte[in.available()]; in.read(data); return Base64.encodeBase64String(data); } /** * 解码 * @param base64Str * @return * @throws IOException */ public byte[] decode(String base64Str)throws IOException{ return Base64.decodeBase64(base64Str); } /** * 随机数 * @return */ public String randNumber(){ double number=Math.random(); String str= String.valueOf(number); str=str.replace(".",""); return str; } public static void main(String[] args){ try { //分块 //new FileSplit().splitByNumber("E:\\20160824134947.jpg",10); //合并 //new FileSplit().mergeByName("E:\\result\\"); }catch (Exception e){ e.printStackTrace(); } } }
更新,不用Base64编码,将小文件信息写入新的文件
<!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency>
import com.alibaba.dubbo.common.utils.ConfigUtils;
import java.io.*;
import java.util.Properties;
/**
* Created by Liwj on 2016/8/23.
*/
public class FileSplit {
private static int length=8192;
/**
* 分割
* @param fileName 文件路径
* @param Number 分块文件个数
* @throws Exception
*/
public void splitByNumber(String fileName,int Number) throws Exception{
File oldFile=new File(fileName);
String infoFilePath=this.getClass().getResource("").getPath();
System.out.println(infoFilePath);
File fileInfo=new File(infoFilePath+"info.properties");
BufferedOutputStream outInfo=new BufferedOutputStream(new FileOutputStream(fileInfo));
outInfo.write(new String("fileName="+oldFile.getName()+"\n").getBytes());
outInfo.write(new String("fileNumber="+Number+"\n").getBytes());
InputStream in=new FileInputStream(oldFile);
System.out.println("分块前-->文件大小:"+oldFile.length()+"字节");
int size=(int)oldFile.length()/Number;
long position;
for(int i=0;i<Number;i++){
if(i==Number-1){
size=(int)oldFile.length()-(Number-1)*size;
}
String newFilePath="E:/result/"+randNumber()+".file";
outInfo.write(new String("file"+i+"="+newFilePath+"\n").getBytes());
File newFile=new File(newFilePath);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(newFile));
byte[] buf=new byte[size];
in.read(buf);
out.write(buf);
out.close();
}
in.close();
outInfo.close();
return;
}
/**
* 文件合并
* @throws Exception
*/
public void mergeByName() throws Exception{
Properties properties= ConfigUtils.loadProperties("info.properties");
int number=Integer.parseInt(properties.getProperty("fileNumber"));
File oldFile=new File("E:/answer/"+properties.getProperty("fileName"));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(oldFile));
for(int i=0;i<number;i++){
File smallFile=new File(properties.getProperty("file"+i));
BufferedInputStream in=new BufferedInputStream(new FileInputStream(smallFile));
byte[] buf=new byte[in.available()];
in.read(buf);
out.write(buf);
in.close();
}
out.close();
}
/**
* 随机数
* @return
*/
public String randNumber(){
double number=Math.random();
String str= String.valueOf(number);
str=str.replace(".","");
return str;
}
public static void main(String[] args){
try {
//分块
//new FileSplit().splitByNumber("E:\\tests\\sw.jpg",10);
//合并
//new FileSplit().mergeByName();
}catch (Exception e){
e.printStackTrace();
}
}
}