Split-文件的切割

package cn.itcast.io.split;

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

public class SplitFile {

	private static final int SIZE = 1024*1024;
	
	public static void main(String[] args) throws IOException {
		
		splitFile_2(new File("青春留言.mp3"));//这个.MP3文件其实不应该用中文,配置的.properties文件中filename无法解析
	}
	/**
	 * 需求:将一个.mp3文件按1M大小进行切割
	 * @param file
	 * @throws IOException
	 */
	public static void splitFile_2(File file) throws IOException {
		
		
		//将源文件和流关联
		FileInputStream fis=new FileInputStream(file);
		
		//创建一个1M的缓冲区
		byte[] buf=new byte[SIZE];
		
		//创建目的
		FileOutputStream fos=null;
		int len=0;
		int count=1;
		
		//为了方便将来合并被切割的碎片文件,需要将切割的碎片文件信息进行保存。使用与流有关的集合Properties
		Properties pro=new Properties();
		
		File dir=new File("e:\\filepart");
		if(!dir.exists())
			dir.mkdirs();
		while((len=fis.read(buf))!=-1){
			
			fos=new FileOutputStream(new File(dir,(count++)+".part"));
			fos.write(buf, 0, len);
			fos.close();
		}
		
		pro.setProperty("partcount", count+"");
		pro.setProperty("filename", file.getName());
		
		fos=new FileOutputStream(new File(dir,count+".properties"));
		pro.store(fos, "saveinfo");
		
		fis.close();
		fos.close();
				
	}
	public static void splitFile_1(File file) throws IOException{
		
		
		//用读取流关联源文件
		FileInputStream fis=new FileInputStream(file);
		
		//创建一个1M大小的缓冲区
		byte[] buf=new byte[SIZE];
		
		//创建目的
		FileOutputStream fos=null;
				
		int len=0;
		int count=1;
		
		File dir=new File("e:\\filepart");
		if(!dir.exists())
			dir.mkdirs();
		
		while((len=fis.read(buf))!=-1){
			
			fos=new FileOutputStream(new File(dir,(count++)+".part"));
			fos.write(buf, 0, len);
		}
		
		fis.close();
		fos.close();
		
	}

}


你可能感兴趣的:(文件的切割)