文件的分割和合并

 
package per.eyuan.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author eyuan(QQ:373048914)<br>
 * 文件分割以及合并<br>
 * 将一个文件分割为指定大小的多个文件<br> 
 * 形如file.mp3.zip分割为file.mp3.zip0.cut和file.mp3.zip1.cut等<br>
 */
public class FileCut {
	/**
	 * main()
	 */
	public static void main(String[] args) {
		FileCut fc = new FileCut();

		String fileName = "D:\\zip\\a.mp3";
		// 1024*1024,1MB
		int size = 1048576;
		//文件分割
		// fc.cut(fileName, size);
		String source = "D:\\zip\\a.mp31.cut";
		try {
			//文件合并
			fc.unCut(source);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 将指定的文件分割为指定大小的多个文件 size为(1024)kb的倍数<br>
	 * @param String source 要分割的文件的路径
	 * @param int size 分割文件的大小,1024字节的整数倍
	 * @return boolean flag 操作是否成功
	 */
	public boolean cut(String source, int size) {
		boolean flag = true;
		// 分割后的文件个数
		int count;
		// 读取number个byte[]后,完成一个文件的输出
		int number = size / 1024;
		// 读取字节流
		byte[] buff = new byte[1024];
		int len;
		// 文件输入输出流
		FileInputStream fis;
		FileOutputStream[] fos;

		File[] dfs;
		File sf = new File(source);

		if (!sf.exists())
			return false;
		if (sf.isDirectory())
			return false;
		// 创建输出文件和输出文件流
		count = (int) sf.length() / size + 1;
		dfs = new File[count];
		fos = new FileOutputStream[count];
		for (int i = 0; i < count; i++) {
			dfs[i] = new File(source + i + ".cut");
			try {
				fos[i] = new FileOutputStream(dfs[i]);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		// 读取源文件,输出到目标文件
		try {
			fis = new FileInputStream(sf);
			// 输出文件的个数
			for (int i = 0; i < count; i++) {
				// 每个文件要读的次数
				for (int j = 0; j < number; j++) {
					// 是否读完
					if ((len = fis.read(buff)) != -1)
						fos[i].write(buff, 0, len);
					else
						break;
				}
				fos[i].close();
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 组合文件<br>
	 * 在同一目录下,寻找和给定文件类似的文件分片,并将其组合起来<br>
	 * 例如,给定D:\zip\a.mp31.cut,则在同一目录下,寻找D:\\zip\\a.mp3*.cut的文件<br>
	 * 并将其按顺序重新组合起来<br>
	 * 假设分割后的文件序号在0-9之间
	 */
	public boolean unCut(String source) throws IOException {
		boolean flag = true;
		File sf = new File(source);
		// 判断路径是否正确
		if (!sf.exists()) {
			System.out.println("文件不存在");
			return false;
		}
		if (sf.isDirectory()) {
			System.out.println("文件为目录");
			return false;
		}
		// 读取字节流
		byte[] buff = new byte[1024];
		int len;
		// 文件名匹配,正则表达式
		// D:\zip\a.mp31.cut,前缀为D:\zip\a.mp3
		String prefix = source.substring(0, source.length() - 5);
//		System.out.println("目标文件路径:" + prefix);
		String fn = sf.getName();
		String c = fn.substring(0, fn.length() - 5) + "[0-9]" + ".cut";
//		System.out.println("正则表达式:" + c);
		Pattern pattern = Pattern.compile(c);
		// 获取所在目录
		String path = sf.getParent();
//		System.out.println("所在目录:"+path);
		File pf = new File(path);
		String[] fs = pf.list();
		// 文件输入、输出流
		FileInputStream fis = null;
		File df = new File(prefix);
		FileOutputStream fos;
		fos = new FileOutputStream(df);
		// 遍历该目录下的所有文件
		for (String s : fs) {
			// System.out.println("文件列表:"+s);
			Matcher matcher = pattern.matcher(s);
			if (matcher.find()) {
//				System.out.println("匹配文件:" + s);
				sf = new File(path + "\\" + s);
				fis = new FileInputStream(sf);
				while ((len = fis.read(buff)) != -1)
					fos.write(buff, 0, len);
				fis.close();
			}
		}
		fos.close();
		return flag;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值