Java 文件分块及合并
Posted 13rj1115
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); } } }
以上是关于Java 文件分块及合并的主要内容,如果未能解决你的问题,请参考以下文章
合并以 XXXXX.csv.gz_1_2.tar 和 XXXXX.csv.gz_2_2.tar 格式分块的两个文件(使用 python 或 pyspark)