java 分块读文件_Java 文件分块及合并

该博客介绍了如何使用Java实现大文件的分块读取和合并。通过Base64编码处理小文件,记录大文件信息和小文件顺序。文章首先展示了使用Apache Commons Codec库进行Base64编码和解码的方法,然后更新了不依赖Base64编码的解决方案,改用Dubbo的ConfigUtils读写文件信息。
摘要由CSDN通过智能技术生成

利用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值