java文件切割

在main方法里,有文件合并与分割的调用方式

package edu.gxcme.sopping.advice;

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

public class SplitUtil {

	/**
	 * getWrite 指定每一份文件的边界 写入 不同的文件 fileString:源文件, index: 文件顺序的标识 bewgin 开始指针的位置
	 * end 指针结束的位置
	 */
	public static Long getWrite(String file, int index, Long begin, Long end) {
		Long endPointer = 0L;
		RandomAccessFile out = null;
		RandomAccessFile in = null;
		try {
			// 声明切割后的文件的磁盘
			in = new RandomAccessFile(new File(file), "r");
			// 切割成临时文件
			out = new RandomAccessFile(new File(file + "_" + index + ".tmp"), "rw");
			byte[] b = new byte[1024];
			// 初始化初始位置
			in.seek(begin);

			int n = 0;
			while (in.getFilePointer() < end && (n = in.read(b)) != -1) {
				out.write(b, 0, n);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return endPointer;
	}

	/*
	 * filePath:文件路径 count:文件个数
	 */
	public static void getSplitFile(String filePath, int count) {
		// 预分配文件空间 r只读 w只写 rw读写
		try {
			RandomAccessFile ref = new RandomAccessFile(new File(filePath), "r");
			// 计算文件的长度
			Long lenght = ref.length();
			// 计算每一份文件的大小
			Long maxSize = lenght / count;
			// 文件的偏移量
			Long offSet = 0L;
			// 开始切割
			for (int i = 0; i < count - 1; i++) {
				// 标记初始化
				Long fbegin = offSet;
				// 分割到第几个文件
				Long fend = maxSize * (i + 1);
				// 写出去
				offSet = getWrite(filePath, i, fbegin, fend);

			}
			// 剩余文件部分,单独用一个文件
			if (lenght - offSet > 0) {
				getWrite(filePath, count - 1, offSet, lenght);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 文件合并 File 合并后的文件 tempFile 分割的文件 tempCount 合并文件的个数
	 * 
	 */
	public static void merge(String file, String tempFile, int tempCount) {
		try {
			// 文件预留空间
			RandomAccessFile ref = new RandomAccessFile(new File(file), "rw");
			// 开始合并文件
			for (int i = 0; i < tempCount; i++) {
				RandomAccessFile reader = new RandomAccessFile(new File(tempFile + "_" + i + ".tmp"), "r");
				byte[] b = new byte[1024];
				int temp = 0;
				while ((temp = reader.read(b)) != -1) {
					ref.write(b, 0, temp);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
                //文件分割,分割成1份
		getSplitFile("D:\\aa\\0.jpg",1);
                //文件合并,有一份分割出来的文件
		merge("D:\\aa\\0.jpg", "D:\\aa\\0.jpg", 1);
                //小文件最好不用文件分割
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值