1. 程式人生 > >JAVA檔案切割和復原

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);
	}
}