JAVA基础—文件的切割与合并

需求
1、将指定文件分割成指定大小的碎片文件
2、将碎片文件合并成完整的文件
分析
1、将指定文件分割成指定大小的碎片文件
:一个文件 目的:多个文件
根据指定碎片大小创建缓冲区数组,用来存储各指定长度的分片文件。循环创建字节流输出流,关联各>个碎片文件。
2、将碎片文件合并成完整的文件
:多个文件 目的:一个文件
创建多个字节输入流关联碎片文件,合并成序列流,可以直接是Vector集合,来获取Enumeration对象,但是效率较低。
也可以使用ArrayList集合,通过迭代器获取Enumeration对象

方法一:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
 
public class FileSplieDemos {
 
	public static void main(String[] args) throws Exception 
	{
		File theReadPath = new File("C:\\Users\\Administrator\\Desktop\\测试文件夹\\凤舞九天.mp3");
		String  theOutPath ="C:\\Users\\Administrator\\Desktop\\测试文件夹\\abc\\";
		// 调用自定义方法将一个图片文件进行切割
		SplieFile(theReadPath,theOutPath,new File(".mp3"));
		//调用自定义方法将这些被分割的文件合并起
		String theMergerpathout = "C:\\Users\\Administrator\\Desktop\\测试文件夹\\abCOPY\\";
		String themergerInputpath="C:\\Users\\Administrator\\Desktop\\测试文件夹\\abc\\";
		String theoutformat = "MergerCopy.mp3";
		MergerFile(themergerInputpath,theMergerpathout,theoutformat);
	}
 
	
 
	private static void MergerFile(String themergerInputpath,
			String theMergerpathout, String theoutformat) throws Exception {
		// TODO Auto-generated method stub
		File fil = new File(theMergerpathout);
		if(!(fil.isDirectory()))
		{
			fil.mkdirs();
		}
		//将文件来源封装对象
		File file = new File(themergerInputpath);
		//获取所有块片的文件
		File[] names = file.listFiles();
		//创建一个容器,将所有的文件名字储存
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		for(int x=0;x<names.length;x++)
		{
			v.add(new FileInputStream(names[x]));
		}
		Enumeration<FileInputStream> en = v.elements();
		//合并多个流对象
		SequenceInputStream sis = new SequenceInputStream(en);
		//创建输出流对象
		BufferedOutputStream bos = 
				new BufferedOutputStream(new FileOutputStream(theMergerpathout+theoutformat));
		int len = 0;
		byte[] b = new byte[1024*1024];
		while((len=sis.read(b))!=-1)
		{
			bos.write(b);
			bos.flush();
		}
	}
 
	private static void SplieFile(File file,String thepath,File format)
	{
		// 通过字节流并联需要切割的文件对象,并加入缓冲技术提高效率
		System.out.println("切割文件功能启动");
		File fil = new File(thepath);
		if(!(fil.isDirectory()))
		{
			fil.mkdirs();
		}
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		int number = 0;
 
			try {
				bis = new BufferedInputStream(new FileInputStream(file));
				//初使化输出路径
				
				byte[] b = new byte[1024*1024];
			    int len  = 0;
			    while((len = bis.read(b))!=-1)
			    {
			    	number++;
			    	bos = new BufferedOutputStream(new FileOutputStream(thepath+number+format));
			    	bos.write(b);
			    	bos.flush();
			    	
			    }
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
		
	}
}
 方法二:
 /*
 * 文件的切割和合并
 * 1、切割
 * 源:一个文件
 * 目的:多个文件
 * 创建缓冲区数组,循环读取被切割文件,存到缓冲区数组中,然后写到新的文件中
 * 2、合并
 * 源:多个文件
 * 目的:一个文件
 * 将多个文件流合并成以序列流,然后读取到输出流中。写到目标文件中
 * */
import java.io.*;
import java.util.*;
 
class SandM
{
	//切割文件
	//指定切割大小
	public static void splitFile(File f, int size)throws IOException
	{
		//创建字节输入流对象,与被切割文件相关联
		FileInputStream fis = new FileInputStream(f);
 
		//创建字节写入流,与目标文件相关联
		FileOutputStream fos = null;
 
		//创建缓冲区数组,用于存储分片数据
		byte[] buf = new byte[size];
		int len = 0;
		int count = 1;
		while((len = fis.read(buf)) != -1)
		{
			fos = new FileOutputStream("./part/"+(count++)+".part");
			fos.write(buf, 0, len);
			fos.close();
		}
 
		fis.close();
	}
 
	//合并文件
	public static void merge()throws IOException
	{
		//创建集合,存储字节输入流
		//和多个碎片文件相关联
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
 
		for(int i = 1; i < 5; i++)
		{
			al.add(new FileInputStream("./part/"+i+".part"));
		}
 
		//获取迭代器
		final Iterator<FileInputStream> it = al.iterator();
		//获取Enumeration对象
		Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
			public boolean hasMoreElements()
			{
				return it.hasNext();
			}
			public FileInputStream nextElement()
			{
				return it.next();
			}
		};
 
		//序列流
		SequenceInputStream sis = new SequenceInputStream(en);
		
		//字节输出流
		FileOutputStream fos = new FileOutputStream("part/merge.java");
 
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf)) != -1)
		{
			fos.write(buf, 0, len);
		}
 
		fos.close();
		sis.close();
	}
 
	public static void main(String[] args)throws IOException
	{
		//创建被分割的文件
		File src = new File("FileDemo.java");
		int size = 1024;
 
		splitFile(src, size);
		merge();
 
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟吶李唦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值