[IO]——文件的分割与合并

来源:互联网 发布:excel恢复昨天的数据 编辑:程序博客网 时间:2024/05/20 09:27
public class SplitFile {//文件的路径private String filePath;//文件名private String fileName;//文件大小private long length;//块数private int size;//每块的大小private long blockSize;//分割后的存放目录private String destBlockPath;//每块的名称private List<String> blockPath;public SplitFile(){blockPath = new ArrayList<String>();}public SplitFile(String filePath,String destBlockPath){this(filePath,destBlockPath,1024);}public SplitFile(String filePath,String destBlockPath,long blockSize){this();this.filePath= filePath;this.destBlockPath =destBlockPath;this.blockSize=blockSize;init();}/** * 初始化操作 计算 块数、确定文件名 */public void init(){File src =null;//健壮性if(null==filePath ||!(((src=new File(filePath)).exists()))){return;}if(src.isDirectory()){return ;}//文件名this.fileName =src.getName();//计算块数 实际大小 与每块大小this.length = src.length();//修正 每块大小if(this.blockSize>length){//输入错误时修正!!!this.blockSize =length;}//确定块数size= (int)(Math.ceil(length*1.0/this.blockSize));//确定文件的路径initPathName();}private void initPathName(){for(int i=0;i<size;i++){this.blockPath.add(destBlockPath+"/"+this.fileName+i+".txt");}}/** * 文件的分割 * 0)、第几块 * 1、起始位置 * 2、实际大小 * @param destPath 分割文件存放目录 * @throws IOException  */public void split() throws IOException{long beginPos =0; //起始点long actualBlockSize =blockSize; //实际大小//计算所有块的大小、位置、索引for(int i=0;i<size;i++){if(i==size-1){ //最后一块actualBlockSize =this.length-beginPos;}spiltDetail(i,beginPos,actualBlockSize);beginPos+=actualBlockSize; //本次的终点,下一次的起点}}/** * 文件的分割 输入 输出 * 文件拷贝 * @param idx 第几块 * @param beginPos 起始点 * @param actualBlockSize 实际大小 * @throws IOException  */private void spiltDetail(int idx,long beginPos,long actualBlockSize) throws IOException{//1、创建源File src = new File(this.filePath);  //源文件File dest = new File(this.blockPath.get(idx)); //目标文件//2、选择流java.io.RandomAccessFile raf = null;  //输入流BufferedOutputStream bos=null; //输出流try {raf=new java.io.RandomAccessFile(src,"r");bos =new BufferedOutputStream(new FileOutputStream(dest));//读取文件raf.seek(beginPos);//缓冲区byte[] flush = new byte[1024];//接收长度int len =0;while(-1!=(len=raf.read(flush))){if(actualBlockSize-len>=0){ //查看是否足够//写出bos.write(flush, 0, len);actualBlockSize-=len; //剩余量}else{ //写出最后一次的剩余量bos.write(flush, 0, (int)actualBlockSize);break;}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{bos.close();raf.close();}}/** * 文件的合并 * @throws IOException  */public void merge(String destPath) throws IOException{//创建源File dest =new File(destPath);//选择流BufferedOutputStream bos=null; //输出流try {bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加BufferedInputStream bis = null;for (int i = 0; i < this.blockPath.size(); i++) {bis = new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i))));//缓冲区byte[] flush = new byte[1024];//接收长度int len =0;while(-1!=(len=bis.read(flush))){bos.write(flush, 0, len);}bos.flush();bis.close();}} catch (Exception e) {}finally{bos.close();}}public static void main(String[] args) throws IOException {SplitFile split = new SplitFile("E:/others/good.txt","E:/others/",1);System.out.println(split.size);split.split();split.merge("E:/others/1234.txt");}}

0 0
原创粉丝点击