今天写了一个含配置文件的 文件分割 及 合并 的java程序。

SplitFileDemo(分割)类

package Split;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class SplitFileDemo {

	private static final int PART_SIZE = 1048576;

	/**
	 * 将文件切成1m大小文件碎片。
	 * 
	 *  
	 */
	public static void main(String[] args) throws IOException {
		
		File file = new File("F:\\等一分钟.avi");
		File dir = new File("F:\\splitPart1");
		splitFile(file,dir);

	}

	public static void splitFile(File file, File dir) throws IOException {
		
		FileInputStream fis = new FileInputStream(file);
		if(!dir.exists())
			dir.mkdirs();
		FileOutputStream fos = null;
		
		byte []buf = new byte[PART_SIZE];
		int len = 0;
		int count = 1;
		while((len = fis.read(buf)) != -1){
			fos = new FileOutputStream(new File(dir,(count++)+".myPart"));
			fos.write(buf, 0, len);	
			fos.close();
		}
		FileWriter fw = new FileWriter(new File(dir,"fileInfo.properties"));
		
		Properties pro = new Properties();
		pro.setProperty("partCount", count+"");
		pro.setProperty("fileName", file.getName());
		pro.store(fw, "fileInfo");
		fw.close();
		fis.close();
	}

}


 

=====================================================================================================================================

mergeFileDemo(合并类)

package merge;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;

public class mergeFileDemo {

	/**
	 * 文件合并程序。
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		File dir = new File("F:\\splitPart1");
		mergeFile(dir);

	}

	public static void mergeFile(File dir) throws IOException {
		File[] properties_file = dir.listFiles(new Suffix("properties")); 
		if(properties_file.length != 1)
			throw new RuntimeException("配置文件不存在 或 不唯一");
		
		
		Properties pro = new Properties();
		pro.load(new FileReader(properties_file[0]));
		int count = Integer.parseInt(pro.getProperty("partCount"));
		String file_name = pro.getProperty("fileName");
		
		
 
	    File[] myPart = dir.listFiles(new Suffix(".myPart"));
	    if(myPart.length != count-1)
	    	throw new RuntimeException("碎片文件数目不正确! 应为 "+(count-1)+" 个");
	    	
		
		ArrayList<InputStream> al = new ArrayList<InputStream>();
		FileInputStream fis = null;
		for(int i = 1; i<count ;i++){
			fis = new FileInputStream(new File(dir,i+".myPart"));
			al.add(fis);
		}
		//创建枚举变量 来存取集合中的读入流。
		Enumeration<InputStream> en = Collections.enumeration(al);
		//将枚举变量中的读取流,进行逻辑串联。
		SequenceInputStream sis = new SequenceInputStream(en);
		
		
		FileOutputStream fos = new FileOutputStream(new File(dir,file_name));
		byte []buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!= -1){
			fos.write(buf, 0, len);
			fos.flush();
		}
		fos.close();
		sis.close();
	}

}


 

===========================================================================================================================================================================

过滤器类

package merge;

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

public class Suffix implements FilenameFilter {	
	private String suffix;
	public Suffix(String suffix) {
		super();
		this.suffix = suffix;
	}
	public boolean accept(File dir, String name) {

		return name.endsWith(suffix);
	}

}


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值