Javaはファイルを複数のサブファイルに分割してサブファイルを元のファイルにマージする

4040 ワード

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Set;

public class Test
{
	public static void main(String[] args) throws Exception
	{
		/*
		*            ,          
		*     ,    1M buffer ,      Part   ,             
		*/

		String sourceFilePath = "D:" + File.separator + "Code" + File.separator + "source" + File.separator + "031316_【 13 :Java  】_   :Properties.wmv";
		int partFileLength = 1024*1024;//           1M
		splitFile(sourceFilePath,partFileLength);//     
		combineFile("D:" + File.separator + "Code" + File.separator + "target");//         
	}

	public static void combineFile(String directoryPath) throws Exception
	{
		Properties config = new Properties();
		InputStream ips = null;
		ips = new FileInputStream(new File(directoryPath + File.separator + "config.properties"));
		config.load(ips);

		Set keySet = config.keySet();//   keySet   int 
		
		
		// keySet    ,   int   set,       
		Set intSet = new TreeSet();
		Iterator iterString = keySet.iterator();
		while(iterString.hasNext())
		{
			String tempKey = (String)iterString.next();
			if("name".equals(tempKey))
			{}
			else
			{
				int tempInt ;
				tempInt = Integer.parseInt(tempKey);
				intSet.add(tempInt);
			}
		}
		
		Set sortedKeySet = new TreeSet();
		sortedKeySet.addAll(intSet);

		OutputStream eachFileOutput = null;
		eachFileOutput = new FileOutputStream(new File("D:" + File.separator + "Code" + File.separator + config.getProperty("name")));

		Iterator iter = sortedKeySet.iterator();
		while(iter.hasNext())
		{
			String key = new String("" + iter.next());
			if(key.equals("name"))
			{}
			else
			{
				//System.out.println("debug---");
				String fileNumber = null;
				String filePath = null;
				fileNumber = key;
				filePath = config.getProperty(fileNumber);

				//       -->     

				InputStream eachFileInput = null;
				
				eachFileInput = new FileInputStream(new File(filePath));
				
				byte[] buffer = new byte[1024*1024*1];//        1M
				int len = 0;
				while((len = eachFileInput.read(buffer,0,1024*1024*1)) != -1)
				{
					eachFileOutput.write(buffer,0,len);
				}
				eachFileInput.close();
			}
		}

		eachFileOutput.close();
	}

	public static void splitFile(String sourceFilePath,int partFileLength) throws Exception 
	{
		File sourceFile = null;
		File targetFile = null;
		InputStream ips = null;
		OutputStream ops = null;
		OutputStream configOps = null;//                  ,                 ,         
		Properties partInfo = null;//properties           
		byte[] buffer = null;
		int partNumber = 1;
		sourceFile = new File(sourceFilePath);//     
		ips = new FileInputStream(sourceFile);//             
		configOps = new FileOutputStream(new File("D:" + File.separator + "Code" //    
			+ File.separator + "target" + File.separator + "config.properties"));
		buffer = new byte[partFileLength];//      
		int tempLength = 0;
		partInfo = new Properties();//key:1       value:    

		while((tempLength = ips.read(buffer,0,partFileLength)) != -1) 
		{
			String targetFilePath = "D:" + File.separator + "Code" 
				+ File.separator + "target" + File.separator + "part_" + (partNumber);//        +   
			partInfo.setProperty((partNumber++)+"",targetFilePath);//        properties
			targetFile = new File(targetFilePath);
			ops = new FileOutputStream(targetFile);//     
			ops.write(buffer,0,tempLength);//         

			ops.close();//      
		}
		partInfo.setProperty("name",sourceFile.getName());//      
		partInfo.store(configOps,"ConfigFile");// properties        
		ips.close();//      
	}
}