Java IO学习笔记(二)

文件的分割

分割:
1、计算每一块的起始位置
2、对文件进行分割

	public void split() throws IOException {
		long len = src.length();
		int beginPos=0;
		int actualSize = (int)(blockSize>len?len:blockSize);
		for(int i=0;i<size;++i) {
			beginPos=i*blockSize;
			if(i==size-1) {
				actualSize=(int)len;
			}else {
				actualSize=blockSize;
				len-=actualSize;
			}
			RandomAccessFile raf1=new RandomAccessFile(this.src,"r");
			RandomAccessFile raf2=new RandomAccessFile(this.destPaths.get(i),"rw");
			raf1.seek(beginPos);
			byte[] flush = new byte[1024];//缓冲容器
			int length = -1;
			while((length=raf1.read(flush))!=-1) {
				if(actualSize>length) {
					raf2.write(flush,0,length);
					actualSize-=length;
				}else {
					raf2.write(flush,0,actualSize);
					break;
				}
			}
			raf2.close();
			raf1.close();
		}
		
	}

文件的合并

合并:
依次读取每一块文件碎片并以追加的方式写文件即可。

package com.liuyuhe;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class FileMerge {
	public static void merge(String destPath,List<String> destPaths) throws IOException {
		OutputStream os=new BufferedOutputStream(new FileOutputStream(destPath,true));
		for(int i=0;i<destPaths.size();++i) {
			InputStream is = new BufferedInputStream(new FileInputStream(destPaths.get(i)));
			byte[] flush = new byte[1024];
			int len=-1;
			while((len=is.read(flush))!=-1) {
				os.write(flush,0,len);
			}
			os.flush();
			is.close();
		}
		os.close();
	}
}

文件分割与合并(完整代码)

package com.liuyuhe;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileSplit {
	private File src;
	private String destDir;
	private List<String> destPaths;
	private int blockSize;
	private int size;
	public FileSplit(String srcPath,String destDir) {
		this(srcPath,destDir,1024);
	}
	public FileSplit(String srcPath,String destDir,int blockSize) {
		this.src=new File(srcPath);
		this.destDir=destDir;
		this.blockSize=blockSize;
		this.destPaths = new ArrayList<String>();
		init();
	}
	private void init() {
		long length = this.src.length();
		this.size = (int)Math.ceil(length*1.0/blockSize);
		for(int i=0;i<size;++i) {
			this.destPaths.add(this.destDir+"/"+i+"-"+this.src.getName());
		}
	}
	public List<String> getdestPaths() {
		return this.destPaths;
	}
	/**
	   * 分割:
	 *1、计算每一块的起始位置
	 *2、分割 
	 * @throws IOException 
	 */
	public void split() throws IOException {
		long len = src.length();
		int beginPos=0;
		int actualSize = (int)(blockSize>len?len:blockSize);
		for(int i=0;i<size;++i) {
			beginPos=i*blockSize;
			if(i==size-1) {
				actualSize=(int)len;
			}else {
				actualSize=blockSize;
				len-=actualSize;
			}
			RandomAccessFile raf1=new RandomAccessFile(this.src,"r");
			RandomAccessFile raf2=new RandomAccessFile(this.destPaths.get(i),"rw");
			raf1.seek(beginPos);
			byte[] flush = new byte[1024];//缓冲容器
			int length = -1;
			while((length=raf1.read(flush))!=-1) {
				if(actualSize>length) {
					raf2.write(flush,0,length);
					actualSize-=length;
				}else {
					raf2.write(flush,0,actualSize);
					break;
				}
			}
			raf2.close();
			raf1.close();
		}
		
	}
	//测试
	public static void main(String[] args) throws IOException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入文件的路径:");
		String srcPath = scanner.nextLine();
		System.out.println("请输入目标文件夹的路径:");
		String destDir = scanner.nextLine();
		System.out.println("请输入合并后文件的路径:");
		String destPath = scanner.nextLine();
		scanner.close();
		FileSplit fs = new FileSplit(srcPath,destDir);
		fs.split();
		System.out.println("文件分割完成!");
		FileMerge.merge(destPath, fs.getdestPaths());
		System.out.println("文件合并完成!");
	}
	
}

发布了151 篇原创文章 · 获赞 236 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Deep___Learning/article/details/104109059