首页 > 代码库 > Java 文件分块及合并
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 = http://www.mamicode.com/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(); } }}
Java 文件分块及合并
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。