说明:
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