1. 程式人生 > >幾種讀大檔案方法的效率對比測試

幾種讀大檔案方法的效率對比測試

說明:

1、首先呼叫了 generateBigFile() 生成一個大的txt 檔案 a.txt,大小是 1.88G 。

package com.other.test1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;

public class BigFileReaderTest {

	
	private static String filePathName = "F:\\a.txt";
	
	public static void main(String[] args) throws IOException {
		// TODO 自動生成的方法存根

		//generateBigFile();
		readFile1();
		readFile2();
		readFile3();
	}

	/**
	 * 讀大檔案
	 * BufferedReader + char[]
	 * @throws IOException
	 */
    public static void readFile1() throws IOException{
    	
    	long start = System.currentTimeMillis();
    	BufferedReader br = new BufferedReader(new FileReader(filePathName));
    	
    	char[] buff = new char[1024];
    	int len = -1;
    	while( (len = br.read(buff)) != -1 ){
    		//System.out.print(new String(buff, 0, len));
    	}
    	long end = System.currentTimeMillis();
		System.out.println("讀大檔案   BufferedReader + char[], 耗時="+(end-start));	
    }

    /**
     * 讀大檔案
     * FileChannel + ByteBuffer
     * @throws IOException
     */
    private static void readFile2() throws IOException{
		
		long start = System.currentTimeMillis();
		FileChannel fc = new FileInputStream(filePathName).getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		
		while(fc.read(buffer) != -1){
			
			buffer.flip();
			//System.out.print(Charset.forName("UTF-8").newDecoder().decode(buffer));;
			buffer.clear();
		}
		long end = System.currentTimeMillis();
		System.out.println("讀大檔案  FileChannel + ByteBuffer, 耗時="+(end-start));
	}
	
    /**
	 * 讀大檔案
	 * BufferedReader + CharBuffer
	 * @throws IOException
	 */
    public static void readFile3() throws IOException{
    	
    	long start = System.currentTimeMillis();
    	BufferedReader br = new BufferedReader(new FileReader(filePathName));
    	CharBuffer buff = CharBuffer.allocate(1024);
    	while( br.read(buff) != -1 ){
    		buff.flip();
    		//System.out.print(buff);
    		buff.clear();
    	}
    	long end = System.currentTimeMillis();
		System.out.println("讀大檔案   BufferedReader + CharBuffer, 耗時="+(end-start));	
    }
	
//    public static void readFile4() throws IOException{
//    	
//    	long start = System.currentTimeMillis();
//		FileChannel fc = new FileInputStream(filePathName).getChannel();
//		int begin = 0, size = 1024;
//		
//		MappedByteBuffer mappedByteBuffer = 
//		   fc.map(FileChannel.MapMode.READ_ONLY, begin, size);
//		
//		while(mappedByteBuffer.capacity() > 0){
//		
//			begin += mappedByteBuffer.capacity();
//			mappedByteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, begin, size);
//
//		}
//		long end = System.currentTimeMillis();
//		System.out.println("nio讀大檔案   FileChannel + MappedByteBuffer, 耗時="+(end-start));	
//    }
    
	/**
	 * 生成一個大檔案 a.txt
	 * @throws IOException
	 */
	private static void generateBigFile() throws IOException{
		
		long start = System.currentTimeMillis();
		File bigFile = new File(filePathName);
		FileWriter fileWriter = new FileWriter(bigFile);
		for(int i=0;i<100000000;i++){
			fileWriter.write(Math.random()+"\r\n");
		}
		fileWriter.close();
		long end = System.currentTimeMillis();
		System.out.println("生成一個大檔案 a.txt , 耗時="+(end-start));
	}
}

測試結果:
讀大檔案   BufferedReader + char[], 耗時=5160
讀大檔案   FileChannel + ByteBuffer, 耗時=9091
讀大檔案   BufferedReader + CharBuffer, 耗時=42333