几种读大文件方法的效率对比测试

说明:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值