IO流--SequenceInputStream切割(合并)文件--优化(Properties集合相结合)

 切割端的优化:
	
	public static void splitFile(File file,File dir) throws IOException
	{
		/*
		 * 切割文件优化:必须记录被切割文件的名称,以及切割出来的个数
		 * 
		 */
		Properties pro=new Properties();
		
		//用读取流关联源文件		
		FileInputStream fis=new FileInputStream(file);
		
		//定义一个1M的缓冲区,每次切割1M数据到临时文件中
		byte[] buf=new byte[SIZE];
		
		//创建目的
		FileOutputStream fos=null;
		
		//如果目录不存在则创建
		if(!dir.exists())
			dir.mkdir();
		int len=0;
		//切割文件的名字
		int count=1;
		while((len=fis.read(buf))!=-1)
		{
			fos=new FileOutputStream(dir+"\\"+(count++)+".part");
			fos.write(buf,0,len);
			fos.close();
		}
		//将被切割文件的信息保存到properties集合中
		pro.setProperty("partcount", count+"");
		pro.setProperty("filename", file.getName());
		fos=new FileOutputStream(new File(dir,count+".properties"));
		pro.store(fos, "save split info");
		fis.close();
		fos.close();
		
		
	}

 


  自定义过滤器:

         

package cn.itheima.cway.IO;

import java.io.File;
import java.io.FilenameFilter;

public class SuffixFilter implements FilenameFilter {
	
 	private String suffix;
	public SuffixFilter(String suffix)
	{
		this.suffix=suffix;
	}
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		return name.endsWith(suffix);
	}

}

优化后的合并:

 
public static void mergerFile(File dec) throws IOException
	{
		//获取到配置信息文件
		File[] files=dec.listFiles(new SuffixFilter(".properties"));
		if(files.length!=1)
			throw new RuntimeException(dec+"该目录下没有properties文件或者不唯一");
		
		//记录配置文件对象
		File confile=files[0];
		
		//获取该文件的信息
		Properties pro=new Properties();
		FileInputStream fis=new FileInputStream(confile);
		pro.load(fis);
		String filename=pro.getProperty("filename");
		int count=Integer.parseInt(pro.getProperty("partcount"));
		
		//获取该目录下的所有碎片文件
		File[] partFiles=dec.listFiles(new SuffixFilter(".part"));
		
		if(partFiles.length!=(count-1))
		{
			throw new RuntimeException("碎片文件个数不符合要求,应该是"+count+"个");
		}
		ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
		for(int i=0;i<partFiles.length;i++)
		{
			al.add(new FileInputStream(partFiles[i]));
		}
		
		//创建一个集合,用于存放读取到的part文件内容
		/*优化前
		ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
		
		//将part文件读取到集合中
		for(int i=1;i<=4;i++)
		{
			al.add(new FileInputStream(new File(dec,i+".part")));
		}
		*/
		//合并后的文件名:
		File dir=new File(dec,filename);
		if(!dir.exists())
			dir.createNewFile();
		
		//把集合中的元素转换为枚举
		Enumeration<FileInputStream> en=Collections.enumeration(al);
		
		//创建序列化输入流,去读取part文件(碎片文件)的内容
		SequenceInputStream	 sis=new SequenceInputStream(en);
		
		//创建一个输出流,用于保存合并后的文件内容
		FileOutputStream fos=new FileOutputStream(dir);
		
		int len=0;
		byte[] buf=new byte[1024];
		
		while((len=sis.read(buf))!=-1)
		{
			fos.write(buf,0,len);
			
		}
		
		fos.close();
		sis.close();
		
	}




public class Properties
   
   
    
    extends 
    
    Hashtable<
    
    Object,
    
    Object>
   
   

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值