切割合并大文件
2016-07-03 22:50
232 查看
package twotext; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.management.RuntimeErrorException; public class 切割合并大文件 { /* 1.写一个方法 ,void splitBigFile(File file , File path) ,可以把一个大文件file(1G以上) 切分成若干小文件放入path目录下 (为了方便合并文件,需要提供一个配置文件记录所有小文件的 信息和顺序)(效果如图) 2.再写一个方法,void mergerPartFile(File path , File file) 把path目录下切分后的小文件合并成完整的大文件file */ public static void main(String[] args) throws Exception { File file = new File("d:\\加农贝壳-20种投篮练习(流畅).f4v"); File path = new File("G:\\part"); // splitBigFile(file, path); mergerPartFile(path, file); } public static void mergerPartFile(File path,File file) { //找到path目录下的配置文件 ,xxx.confug File[] children = path.listFiles(); if(children ==null||children.length == 0) { throw new RuntimeException("找不到配置文件"); } File configFile = null; for(File child :children) { if(child.isFile()&&child.getName().endsWith(".config")) { configFile = child; break; } } //读取配置信息 FileReader reader = null; BufferedReader bufferedReader = null; //从小文件中读取数据 FileInputStream in = null; FileOutputStream out = null;//把读到的数据,写入大文件 try { reader = new FileReader(configFile); bufferedReader = new BufferedReader(reader); out = new FileOutputStream(file); String line = null; while((line = bufferedReader.readLine())!=null) { //小文件名称 line in = new FileInputStream(new File(path,line)); //文件数据的复制 byte[] buff = new byte[1024*1024]; int len = 0; while((len=in.read(buff))!=-1) { out.write(buff,0,len); } in.close(); } } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } finally { if(out!=null) { try { out.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if(bufferedReader!=null) { try { bufferedReader.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if(reader!=null) { try { reader.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if(in!=null) { try { in.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } public static void splitBigFile(File file,File path) throws Exception { //默认小文件的长度是100M long partFileLength = 1024*1024*100; //参数的有效性检查 if(file==null) { throw new IllegalArgumentException("file不能是null"); } if(!file.exists()) { throw new IllegalArgumentException("file不存在"); } if(file.isDirectory()) { throw new IllegalArgumentException("file不是文件"); } if(path==null) { throw new IllegalArgumentException("path不能是null"); } if(!path.exists()) { path.mkdirs(); } if(path.isFile()) { throw new IllegalArgumentException("path不能是文件"); } //文件切割:从大文件中读取数据,写入到多个小文件中 FileInputStream in = null; FileOutputStream out = null; FileWriter configWriter = null; String bigFilename = file.getName(); //big.txt.1.part int index = 1; try { in = new FileInputStream(file); //第一个小文件的输出流 out = new FileOutputStream(new File(path,bigFilename+"."+index+".part")); configWriter = new FileWriter(new File(path,bigFilename+".config")); configWriter.write(bigFilename+"."+index+".part\r\n"); index++; //文件数据复制 byte[] buff = new byte[1024*128];//128kb int len = 0; //每个小文件已经写入数据长度 long totalLength = 0; while(true) { len = in.read(buff); if(len == -1) { break; } out.write(buff,0,len); totalLength+=len; if(totalLength>=partFileLength) { //如果当前小文件的数据已经超过或等于默认小文件的大小,就准备写入下一个小文件 out.close(); out = new FileOutputStream(new File(path,bigFilename+"."+index+".part")); configWriter.write(bigFilename+"."+index+".part\r\n"); index++; totalLength = 0; } } } finally{ if(in!=null) { try { in.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if(out!=null) { try { out.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if(configWriter!=null) { try { configWriter.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } } }
相关文章推荐
- TYVJ 1305 最大字序和
- Java知识点2
- 标准C的标记化结构初始化语法
- C语言文件的编译与执行的四个阶段
- task多任务处理
- mac hbase程序启动警告:找不到hadoop native lib
- Makefile介绍
- 集成ffmpeg/x264:ERROR: libx264 not found的问题
- 离线安装Cloudera
- 网页弹出表格窗口
- 在iOS开发中,需要用到的多线程技术。
- FJ省队集训DAY1 T1
- POJ 3122 二分
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
- ceph 部署后rbd块设备读写只有10M左右,慢的更龟速一样,怎样解决!,求各位博友指点……,感激不尽……
- 2016计蒜之道复赛 微信钱包付款
- POJ 3122 二分
- POJ 2350 Above Average
- Linux之:Ubuntu速学笔记(2)
- SignalR系列教程:在MVC5中使用SignalR