分割合并文件


  
  
   
   
    
    
    
    


    
    


   
   
  
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 spliterDemo {

	private static final int SIZE = 1024*1024;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	
		File file = new File("c:\\a.png");
		File dir = new File("c:\\splitfiles");
//		分割
		splitFile(file);
//		合并
		mergeFile(dir);
//		带配置信息的合并
		mergeFile2(dir);
		
	}
private static void mergeFile2(File dir) throws IOException {
		File[] files = dir.listFiles(new SuffixFilter(".properties"));
		if(files.length!=1)
			throw new RuntimeException("找不到配置信息");
//		碎片文件路径
		File[] partfile = dir.listFiles(new SuffixFilter(".part"));
//		读取文件信息
		Properties prop = new Properties();
		File confile = files[0];
		FileInputStream fis = new FileInputStream(confile);
		prop.load(fis);
		String fileName = prop.getProperty("name");
		int count = Integer.parseInt(prop.getProperty("number"));
//		判断碎片数量
		if(partfile.length!=(count-1))
				throw new RuntimeException("碎片不完整");
//		合并文件
		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
		for(int x=0;x<partfile.length;x++)
			al.add(new FileInputStream(partfile[x]));
		Enumeration<FileInputStream> en = Collections.enumeration(al);
		SequenceInputStream sis = new SequenceInputStream(en);
		FileOutputStream fos = new FileOutputStream(new File(dir,fileName));
		int len =0;
		byte[] buf = new byte[SIZE];
		while((len=sis.read(buf))!=-1){
			fos.write(buf,0,len);
			
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值