`

JAVA 文件的拆分与合并处理

    博客分类:
  • java
 
阅读更多

 

描述: 将一个pdf拆分为两个文件保存,然后在将文件合并成一个文件。

 

package IO.ReaderAndWriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Title:Test1
 * @Author Tony
 * @Date: 2014年6月25日 上午11:33:04
 * @Description:  在读取数据的时候,能用块读取数据尽量一次读取一块,速度相差好多 
 */
public class Test1 {
	public static void main(String[] args) throws UnsupportedEncodingException, IOException {
		wirteFileSplit();
		combinFile();
	}
	
	/**
	 * @throws IOException 
	* @Title: wirteFileSplit 
	* @Description: 写文件 split  ,将一个文件分为两个文件保存 
	* @return void    返回类型 
	* @throws
	 */
	public static void wirteFileSplit() throws IOException{
		
//		File f =new File("F:\\sysmodel.xml");
//		System.out.println( f.getPath() );
		
		File f =new File("F:\\spring-framework-reference.pdf");
		FileOutputStream out1 = new FileOutputStream(new File("F:\\a.tmp"));//保存的第一个文件 
		FileOutputStream out2 = new FileOutputStream(new File("F:\\b.tmp"));//保存的第二个文件 
		Long fileSize = f.length();
		
		FileInputStream in = new FileInputStream(f);
		byte[] buf = new byte[1024];
		int len ; 
		int totle =0 ;
		while( (len= in.read(buf, 0, buf.length ))!=-1 ){
			totle += len;
			if(totle<fileSize/2){
				out1.write(buf, 0, len);
			}else{
				out2.write(buf, 0, len);
			}
		}
		out1.flush();
		out2.flush();
		
		out1.close();
		out2.close();
//		out1.getFD().sync();
//		out2.getFD().sync();
		
		System.out.println("完成");
//		try {
//			FileReader fr = new FileReader("F:\\sysmodel.xml");
//			BufferedReader br  = new BufferedReader(fr);
//			char[] cs = new char[1024];
//			int len=0 ; 
//			int cha ; 
//			String strLine ; 
//			long start = System.currentTimeMillis();
//			//一次读取一个字符列表 ,读取数据尽量选用这个
//			while( (len = fr.read(cs, 0, 1024))!=-1 ){
//				System.out.println(new String( cs,0,len) );
//			}
//			//一次只读取一个字符
//			while((cha = fr.read())!=-1 ){
//				System.out.println( (char)cha);
//			}
//			
//			while((strLine = br.readLine())!=null ){
//				System.out.println( strLine );
//			}
//			
//			long end = System.currentTimeMillis();
//			System.out.println( start  - end );
//			fr.close();
//				
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	
	/**
	 * @throws IOException 
	* @Title: combinFile 
	* @Description: 将拆分的两个文件,合并为一个文件  
	* @return void    返回类型 
	* @throws
	 */
	public static void combinFile() throws IOException{
	 
		FileInputStream in1 = new FileInputStream("F:\\a.tmp");
		FileInputStream in2 = new FileInputStream("F:\\b.tmp");
		
		FileOutputStream out1 = new FileOutputStream(new File("F:\\test.pdf"));//拆分的两个文件合成一个文件 
		
		byte[] buf = new byte[1024];
		int len ; 
	
		while( (len= in1.read(buf, 0, buf.length ))!=-1 ){
			out1.write(buf, 0, len);
		}
		while( (len= in2.read(buf, 0, buf.length ))!=-1 ){
			out1.write(buf, 0, len);
		}	
		
		
		in1.close();
		in2.close();
		out1.flush();
		out1.close();
		System.out.println( "完成 ");
//		out1.getFD().sync();	
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics