JAVA文件切割和复原

一、切割

package cn.itcast.splitfile.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 需求:切割文件
 * 
 * 
 * */
public class SpiltFileDemo {

	private static final int SIZE = 1024*1024;

	public static void main(String[] args) throws IOException {
		File fe=new File("F:\\CloudMusic\\薛之谦 - 绅士.mp3");
//		getfile_1(fe);
		getfile_2(fe);
	}
	//方法二,增加了配置文件
	public static void getfile_2(File fe) throws IOException {
		FileInputStream fi=new FileInputStream(fe);
		File dir=new File("F:\\CloudMusic\\partfile");
		
		if(!dir.exists())
			dir.mkdirs();
		FileOutputStream fo=null;
		byte[]buff=new byte[SIZE];
		int len=0;
		int count=1;
		while((len=fi.read(buff))!=-1) {
			fo=new FileOutputStream(new File(dir,(count++)+ ".part"));
			fo.write(buff, 0, len);
			fo.close();
		}
		
		//属性集合
		Properties pr=new Properties();
		pr.setProperty("num", Integer.toString(count));
		pr.setProperty("name", fe.getName());
		//创建配置文件
		File pf=new File(dir,(count++)+".properties");
		fo=new FileOutputStream(pf);//配置文件和输出流关联 
		//将属性集合里面的数据存入到输出流中
		pr.store(fo, "file splite info");
		fi.close(); 
		fo.close();	
	}
	//方法一
	public static void getfile_1(File fe) throws IOException {	
		FileInputStream fi=new FileInputStream(fe);//输入流和文件相连接
		File dir=new File("F:\\CloudMusic\\partfile");//创建目标目录
		if(!dir.exists())
			dir.mkdirs();
		FileOutputStream fo=null;//定义输出流
		byte[]buff=new byte[SIZE];
		int len=0;
		int count=1;
		while((len=fi.read(buff))!=-1) {
			fo=new FileOutputStream(new File(dir,(count++)+ ".part"));
			fo.write(buff, 0, len);
			fo.close();
		}
		fi.close(); 
	}
}

二、复原

package cn.itcast.mergefile.demo;
/**
 * 将切割的文件合并
 * 
 * */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;

public class MergeFileDemo {

	public static void main(String[] args) throws IOException {
		
		File dir=new File("F:\\CloudMusic\\partfile");
//		putfile_1(dir);
		putfile_2(dir);
		
	}

	public static void putfile_2(File dir) throws IOException {
		
		//读取配置文件信息
		File[]fp=dir.listFiles(new Bynamefilter(".properties"));
		//健壮性判断
		if(fp.length!=1)
			throw new RuntimeException("配置文件不存在或者不止一个");
		//将配置文件信息封装为对象
		File config=fp[0];
		//创建属性集合
		Properties pf=new Properties();
		//将数据从流中保存到属性集合
		pf.load(new FileInputStream(config));
		//获取配置信息
		String name=pf.getProperty("name");
		int num=Integer.parseInt(pf.getProperty("num"));
		//获取碎片文件
		File[]fpart=dir.listFiles(new Bynamefilter(".part"));
		if(fpart.length!=(num-1))
			throw new RuntimeException("碎片文件数目不对,应该是"+num+"个");
		//创建集合保存碎片文件
		ArrayList<FileInputStream>ar=new ArrayList<FileInputStream>();
		for(int x=0;x<fpart.length;x++) {
			ar.add(new FileInputStream(fpart[x]));
		}
		//获得枚举
		Enumeration<FileInputStream>en=Collections.enumeration(ar);
		SequenceInputStream sm=new SequenceInputStream(en);
		byte[]buff=new byte[1024];
		int len=0;
		FileOutputStream fo=new FileOutputStream(new File(dir,name));
		while((len=sm.read(buff))!=-1) {
			fo.write(buff, 0, len);
		}
		fo.close();
		sm.close();
		
		
	}
//方法1	
	public static void putfile_1(File dir) throws IOException {
		//创建集合
		ArrayList<FileInputStream>ar=new ArrayList<FileInputStream>();
		//将输入流中 存入集合
		for(int x=1;x<13;x++) {
			ar.add(new FileInputStream(new File(dir,x+".part")));
		}
		//获得枚举
		Enumeration<FileInputStream>en=Collections.enumeration(ar);
		//获得输入序列
		SequenceInputStream sm=new SequenceInputStream(en);
		byte[]buff=new byte[1024];
		int len=0;
		//创建输出流
		FileOutputStream fo=new FileOutputStream(new File(dir,"薛之谦 - 绅士.mp3"));
		//将序列流中的数据存入到输出流中
		while((len=sm.read(buff))!=-1) {
			fo.write(buff, 0, len);
		}
		fo.close();
		sm.close();
	}
}

过滤器

package cn.itcast.mergefile.demo;
//过滤器,以指定字符窜结尾
import java.io.File;
import java.io.FilenameFilter;

public class Bynamefilter implements FilenameFilter {
	
	private String end;
	public Bynamefilter(String end) {
		super();
		this.end = end;
	}
	@Override
	public boolean accept(File dir, String name) {

		return name.endsWith(end);
	}
}

猜你喜欢

转载自blog.csdn.net/TDOA1024/article/details/82689117
今日推荐