java文件 分割 合并

1、文件合并分割例子

package com.lhf.file1;

import java.io.File;

public class FileMain {

	public static void main(String[] args) throws Exception {

		  //分割文件
        //FileSliptUtil.splitFileDemo(new File("E:\\work\\20200729\\test\\ueltraedutchinese _3_test.rar"), 2);
        
        //合并文件
        FileSliptUtil.joinFileDemo(new String[]{"E:\\work\\20200729\\test\\ueltraedutchinese _3_test_data1.rar", "E:\\work\\20200729\\test\\ueltraedutchinese _3_test_data2.rar"});

	}

}

package com.example.filedemo.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileSplitUtil {

	/**
	 *
	 * @Description 文件分割=
	 * @param src 分割文件路径
	 * @param m   大小
	 * @throws IOException
	 */
	public static void splitFileDemo(File src, int m) throws IOException {
		if (src.isFile()) {
			// 获取文件的总长度
			long fileLength = src.length();
			// 获取文件名
			String fileName = src.getName().substring(0, src.getName().indexOf("."));
			// 获取文件后缀
			String endName = src.getName().substring(src.getName().lastIndexOf("."));
			System.out.println(endName);
			InputStream in = null;
			try {
				in = new FileInputStream(src);
				for (int i = 1; i <= m; i++) {
					StringBuffer sb = new StringBuffer();
					sb.append(src.getParent());
					sb.append("\\");
					sb.append(fileName);
					sb.append("_data");
					sb.append(i);
					sb.append(endName);
					System.out.println(sb.toString());

					File file2 = new File(sb.toString());
					// 创建写文件的输出流
					OutputStream out = new FileOutputStream(file2);
					int len = -1;
					byte[] bytes =null;
					if(fileLength<10 * 1024 * 1024){
						bytes = new byte[10 * 1024];
					}else{
						bytes = new byte[10 * 1024 * 1024];
					}
					while ((len = in.read(bytes)) != -1) {
						out.write(bytes, 0, len);
						if (file2.length() > (fileLength / m)) {
							break;
						}
					}
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (in != null)
					in.close();
			}
			System.out.println("--- 文件分割完成 ---");
		}
	}


	/**
	 *
	 * @Description 文件合并的方法 改进后的方法
	 * @param src 合并文件路径
	 */
	public static void joinFileDemo(String[] src) {
		// 获取合并文件
		File newFile = new File(src[0].toString());
		// 获取文件名 后缀
		String fileName = newFile.getName().substring(0, newFile.getName().indexOf("_"));
		String endName = newFile.getName().substring(newFile.getName().lastIndexOf("."));
		// 得到新的文件名
		StringBuffer sb = new StringBuffer();
		sb.append(newFile.getParent());
		sb.append("\\");
		sb.append(fileName);
		sb.append(endName);
		newFile = new File(sb.toString());
		for (int i = 0; i < src.length; i++) {
			File file = new File(src[i]);
			try {
				// 读取小文件的输入流
				InputStream in = new FileInputStream(file);
				OutputStream out = new FileOutputStream(newFile, true);
				int len = -1;
				byte[] bytes = new byte[10 * 1024 * 1024];
				while ((len = in.read(bytes)) != -1) {
					out.write(bytes, 0, len);
				}
				out.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("文件合并完成!");
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值