JAVA 학습 제55과 - IO 흐름 (9) 파일 절단기

4255 단어
파일 절단기
private static final int SIZE = 1024 *1024;
	public static void splitFile(File file) throws IOException{
		
		//        (       )
		
		FileInputStream fis = new FileInputStream(file);//    
		
		byte[] by = new byte[SIZE];//  1M    
		
		FileOutputStream fos = null;//        
		
		int len = 0;
		int count = 1;//       
		
		File dir = new File("D:\\patFiles");
		if(!dir.isFile()){
			dir.mkdirs();
		}
		
		while((len = fis.read(by))!=-1){
			fos = new FileOutputStream(new File(dir,(count++)+".part"));//       
			fos.write(by,0,len);
		}
		fos.close();
		fis.close();
	}

파일 병합
public static void main(String[] args) throws IOException {
		
		File file = new File("D:\\PartFile");
		Merge(file);
	}
	public static void Merge(File dir)throws IOException{
		
		ArrayList<FileInputStream> AL = new ArrayList<FileInputStream>();
		
		for(int i = 1;i<=7;i++){
			AL.add(new FileInputStream(new File(dir,i+".part")));
		}
		
		Enumeration<FileInputStream> en = Collections.enumeration(AL);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,"    .mp3"));
		
		byte[] by = new byte[1024];
		int len = 0;
		while((len = sis.read(by))!=-1){
			fos.write(by, 0, len);
		}
		sis.close();
		fos.close();
	}

파일 가공 결합 + 구성 파일
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;

public class Main 
{
		private static final int SIZE = 1024 *1024;
	public static void main(String[] args) throws IOException {
		
		File file1 = new File("d:\\NeedSplit\\    .mp3");
		File file2 = new File("D:\\PartFiles");
		splitFile(file1);
		Merge_1(file2);
	}
	
	public static void splitFile(File file) throws IOException{
		
		//        (       )
		
		FileInputStream fis = new FileInputStream(file);//    
		
		byte[] by = new byte[SIZE];//  1M    
		
		FileOutputStream fos = null;//        
		
		int len = 0;
		int count = 1;//       
		
		/*                             ,    
		 *           ,        ,    Properties  */
		
		Properties pro = new Properties();
		
		
		File dir = new File("D:\\PartFiles");
		if(!dir.isFile()){
			dir.mkdirs();
		}
		
		while((len = fis.read(by))!=-1){
			fos = new FileOutputStream(new File(dir,(count++)+".part"));//       
			fos.write(by,0,len);
			fos.close();
		}
		//            pro   
		pro.setProperty("partCount", count+"");
		pro.setProperty("fileName", file.getName());
		fos = new FileOutputStream(new File(dir,count+".properties"));
		// pro           
		pro.store(fos, "save file infmation");
		fis.close();
	}
	
	public static void Merge_1(File dir)throws IOException{
		
		//             
		File[] files = dir.listFiles(new SuffixFilter(".properties"));//new     
		if(files.length!=1){
			throw new RuntimeException(dir+"      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 = dir.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]));
		}
		
		//            
		Enumeration<FileInputStream> en = Collections.enumeration(AL);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		//    
		FileOutputStream fos = new FileOutputStream(new File(dir,filename));
		byte[] by = new byte[1024];
		int len = 0;
		while((len = sis.read(by))!=-1){
			fos.write(by, 0, len);
		}
		sis.close();
		fos.close();
	}
}

좋은 웹페이지 즐겨찾기