文件的切割与合并(视频文件或者压缩文件)

本文介绍了一个简单的Java程序,用于将大文件分割成多个小文件,并提供了重新组合这些小文件的功能。该程序首先将一个197MB的ZIP文件分割成五部分,然后将这些部分合并回原始文件。

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

public class Test {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		try {
			segmentation();   //文件的切割
			aggregation();	 //文件的合并	
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public static void segmentation() throws IOException{
		//选中的文件地址是       D:\soft\screencapture.zip 大小为197MB
		File file=new File("D:\\soft\\screencapture.zip");
		InputStream in=new FileInputStream(file);	
		OutputStream out=null;
		byte[] bytes=new byte[(int) (file.length()/5)+1];	
		System.out.println(file.length());
		for (int i = 0; i < 5; i++) {		
			File file2 =new File("D:\\soft\\screencapture"+i+".zip");
			out=new FileOutputStream(file2);
			int len=in.read(bytes);
			out.write(bytes,0,len);
			
		}
		out.close();
		in.close();
	}
	
	public static void aggregation() throws IOException{			
		InputStream in=null;
		OutputStream out=null;
		for (int i = 0; i < 5; i++) {			
			File file =new File("D:\\soft\\screencapture"+i+".zip");
			in =new FileInputStream(file);
			out=new FileOutputStream("D:\\soft\\新.zip",true);
			int len =-1;
			byte[] b =new byte[10240];
			while ((len=in.read(b))!=-1) {
				out.write(b, 0, len);
			}
		}
		out.close();
		in.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值