用java实现文件的拆分和合并

本文介绍了一种使用Java实现文件拆分和合并的方法,通过将大文件按100KB为单位拆分成多个子文件,然后再次合并这些子文件以恢复原始文件。文章详细展示了如何利用Java的FileInputStream和FileOutputStream类来读取和写入文件,实现了文件的高效管理和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用java实现文件的拆分和合并

找到一个文件,按照100k为单位,拆分成多个子文件,并且以编号作为文件名结束。
比如文件 信息.xlsx,大小是389k。
拆分之后,成为
信息.xlsx-0——100k
信息.xlsx-1——100k
信息.xlsx-2——100k
信息.xlsx-3——89k

而后,对拆分的文件进行合并。
直接上图:

package IODemo.inputStreamOut;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.sun.tools.javac.code.Attribute.Array;

public class SplitorMergeFiles {

	public static void main(String[] args) {
//		拆分
//		File f = new File("D:\\test\\信息.xlsx");
//		split(f);
		
		//合并
		File f = new File("D:\\test\\merge");
		File[] fs = f.listFiles();
		for (File file : fs) {
			System.out.println(file);
		}
		
		File newFile = new File("D:\\test\\merge\\newfile");
		
		merge(fs,newFile);
		
		
	}

	// 拆分文件
	public static void split(File f) {

		byte[] data = new byte[(int) f.length()];
		try {
			FileInputStream fis = new FileInputStream(f);

			fis.read(data);// 读取文件到data中
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(data.length / 1024 + "k");

		int len = 1024 * 100;
		int n = (int) (f.length() / len);// 拆分为多少100k的文件夹
		int m = (int) (f.length() / len);// 余数再额外准备一个文件夹
		File[] fs = new File[n + 1];// 设立文件数组,用来存放文件。长度为总共需要生成文件的个数。

		for (int i = 0; i < n + 1; i++) {

			int start = i * 102400;
			int end = (i + 1) * 102400;
			fs[i] = new File(f + "-" + i);

			if (i < n) {
				byte[] b = Arrays.copyOfRange(data, start, end);

				try {
					FileOutputStream fos = new FileOutputStream(fs[i]);
					fos.write(b);
					fos.close();

				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {

				start = i * 102400;
				end = (int) f.length();
				byte[] b = Arrays.copyOfRange(data, start, end);

				try {
					FileOutputStream fos = new FileOutputStream(fs[i]);
					fos.write(b);
					fos.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			System.out.println("输出子文件夹:" + fs[i].getAbsolutePath() 
				+ "  其长度为: " + fs[i].length());
		}
	}

	// 合并文件
	public static void merge(File[] fs,File newFile) {
		
		int len = 0;
		for (File file : fs) {
			len += file.length(); 
		}
		byte[] b = new byte[len];//构造一个数组,存放合并的数据
		
		int len1 = 0;//初始化复制到目的数组的起始值
		
		for (File file : fs) {
						
			byte[] bi = new byte[(int) file.length()];
			
			try {
				FileInputStream fis = new FileInputStream(file);
				fis.read(bi);
				fis.close();
				System.out.println(bi.length);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.arraycopy(bi, 0, b, len1, (int) file.length());
			len1 +=file.length();
			
			try {
				FileOutputStream fos = new FileOutputStream(newFile);
				fos.write(b);
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 		
		}			
	}
}

最后结果图附上。最后结果图附上。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值