文件的分割与合并
来源:互联网 发布:linux 格式化硬盘 编辑:程序博客网 时间:2024/06/10 17:22
package com.departFile;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.RandomAccessFile;import java.util.ArrayList;import java.util.List;/** * 文件的分割与合并 * 分割 * 1、将块的大小、数量、每块的大小都划分好 * 2、按计划好的大小和块数进行多次划分 * * 合并 * 将子文件按顺序以悬挂的方式写入目标文件中 * @author Administrator * */public class SplitFile{ private long blockSize;//每块大小 private int size;//分割的块数 private String filePathString;//要分割文件的路径 private List<String> subFilePath;//子文件的路径+名字 private long length; public SplitFile() { subFilePath=new ArrayList<String>(); } public SplitFile(String filePathString) { this(1024,filePathString); } public SplitFile(int blockSize, String filePathString) { this(); this.blockSize=blockSize; this.filePathString = filePathString; init(); } /** * 初始化操作 */ public void init() { File src=new File(filePathString); if (filePathString==null|| (!src.exists())) { return; } if (src.isDirectory()) { return; } //开始计算 this.length=src.length();//总大小 if (this.length<blockSize) { blockSize=this.length; } size=(int)Math.ceil(this.length*1.0/blockSize); getSubFilePath(size); } private void getSubFilePath(int n) { for (int i = 0; i < n; i++) { subFilePath.add("d:"+File.separator+i+".txt"); } } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String destpathString="d:\\c.txt"; String srcpathString="d:\\a.txt"; int blockSize=8;//每个块的大小 //long n=0;//块数 SplitFile sFile=new SplitFile(blockSize, srcpathString); System.out.println(sFile.size); sFile.splite(); sFile.mergeFile(destpathString); } /** * 文件的分割 * @throws IOException */ public void splite() throws IOException { long beginPos=0; long actuallBlockSize=blockSize; for (int i = 0; i < size; i++) { if (i==size-1) { actuallBlockSize=this.length-beginPos; } splitDetil(i, beginPos, actuallBlockSize); beginPos+=blockSize; } } /** * 文件的分割细节 * 第index块的写 * @param index * @param beginPos * @param actuallBlockSize * @throws IOException */ public void splitDetil(int index,long beginPos,long actuallBlockSize) throws IOException { //创建源于目的地 File srcFile=new File(this.filePathString); File destFile=new File(this.subFilePath.get(index)); //准备流 RandomAccessFile ra=new RandomAccessFile(srcFile, "r"); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile)); //准备读取 ra.seek(beginPos); //实际大小 int len=0; //缓冲大小 byte[] data=new byte[1024]; while (-1!=(len=ra.read(data))) { //前面的块只读len长度,len不知道大小 if (actuallBlockSize>=len) { bos.write(data, 0, len); actuallBlockSize-=len; }else { bos.write(data, 0, (int)actuallBlockSize); break; } } bos.flush(); if (bos!=null) { bos.close(); } if (ra!=null) { ra.close(); } } /** * 文件的合并 * @param destpathString */ public void mergeFile(String destpathString) { File destFile=new File(destpathString); for (int i = 0; i < subFilePath.size(); i++) { File srcFile=new File(subFilePath.get(i)); try { fileWriteToFile(srcFile,destFile); } catch (IOException e) { e.printStackTrace(); System.out.println("文件拷贝失败"); } } } /** * 文件的拷贝 * @param src * @param dest * @throws IOException */ public static void fileWriteToFile(File src,File dest) throws IOException { //1、选择流 InputStream is=null;//将文件读入程序 OutputStream os=null;//将程序中的文件写出到文件 is=new BufferedInputStream(new FileInputStream(src)); os=new BufferedOutputStream(new FileOutputStream(dest, true)); //准备容器 byte[] data=new byte[1024]; int len=0; while (-1!=(len=is.read(data))) { os.write(data,0,len); } os.flush(); if (is!=null) { is.close(); } if (os!=null) { os.close(); } }}
0 0
- 文件的合并与分割
- 文件的合并与分割
- 文件的分割与合并
- 文件的分割与合并
- 文件的分割与合并
- 文件的分割与合并
- 文件的分割与合并
- 文件分割与合并
- 电影文件的合并与分割
- 如何实现文件的分割与合并?
- Java_文件的分割与合并
- C#文件的分割与合并
- 文件的分割与合并SequenceInputStream
- java实现文件的分割与合并
- php 文件的分割与合并
- 文件的分割(split)与合并(cat)
- 合并分割的文件
- 分割文件与合并文件
- 安装Erlang和Eclipse下的Erlide(Windows)
- [HDU 2066] 多个最短路比较大小
- Tomcat服务器+MySQL数据库+MyBatis持久层框架的简单使用
- 用NginX+keepalived实现高可用的负载均衡
- uefi制作启动盘
- 文件的分割与合并
- (MVP+RxJava+Retrofit)解耦+Mockito单元测试 经验分享
- springMVC搭建总结
- Android自定义视图动画(一)
- Opencv3.1使用教程(一)ubuntu 14.04 安装Opencv3.1.0 (包含opencv_contrib模块)
- UVA - 11100 The Trip, 2007
- spark 自带的例子在eclipse下运行的方法
- AndroidStudio 快速集成fastjson和注解
- 2016"百度之星" - 测试赛 1002 列变位法解密 (模拟)