Java分割合并文件

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

class Fen {
	String file; //要分割的文件名
	int size; //被分割的文件大小
	long[] byteArray;

	Fen(String file, String size) {
		this.file = file;
		this.size = Integer.parseInt(size) * 1024 * 5;
	}
	public int splitFileNumber(int fileLength){
		int number = fileLength / size ;
		int x = fileLength%size > 0 ? 1:0;
		number = number + x;
		byteArray = new long[number];
		for(int i=0;i<fileLength/size;i++){
			byteArray[i] = size;
		}
		if(x == 1) byteArray[number-1] = fileLength%size;
		
		return number;
	}
	public void split() throws Exception {
		File inFile = new File(file);
		int fileLength = (int) inFile.length(); // 取得文件的大小
		int number; // 取得要分割的文件个数

		RandomAccessFile inn = new RandomAccessFile(inFile, "r");// 打开要分割的文件
		number = splitFileNumber(fileLength);

	
		int i = 0;
		long start = 0;
		
		for (int j = 0; j < number; j++) {
			File outFile = new File(inFile.getAbsolutePath() + j);
			System.out.println(outFile.getAbsolutePath());
			RandomAccessFile out = new RandomAccessFile(outFile, "rw");
			start += byteArray[j];
			for (; i < start; i++) {
				out.write(inn.read());
			}
			
			out.close();
		}
		inn.close();
	}
	public void Combination() throws IOException{
		File inFile = new File(file);
		int fileLength = (int) inFile.length(); // 取得文件的大小
		int number; // 取得要分割的文件个数
		File inNewFile = new File(inFile.getParent()+"\\"+"New"+inFile.getName());
		RandomAccessFile out = new RandomAccessFile(inNewFile, "rw");
		number = splitFileNumber(fileLength);
		
		for (int j = 0; j < number; j++) {
			File inFiles = new File(inFile.getAbsolutePath() + j);
			System.out.println(inFiles.getAbsolutePath());
			RandomAccessFile ins = new RandomAccessFile(inFiles, "r");
			
			for (int i = 0; i < inFiles.length(); i++) {
				out.write(ins.read());
			}
			
			ins.close();
		}
		out.close();
	}
}

 

 

 

你可能感兴趣的:(java,J#)