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

来源:互联网 发布:淘宝篮球正品店推荐 编辑:程序博客网 时间:2024/06/03 17:16

说明:

1、首先调用了 generateBigFile() 生成一个大的txt 文件 a.txt,大小是 1.88G 。


[java] view plain copy
  1. package com.other.test1;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.nio.ByteBuffer;  
  10. import java.nio.CharBuffer;  
  11. import java.nio.channels.FileChannel;  
  12.   
  13. public class BigFileReaderTest {  
  14.   
  15.       
  16.     private static String filePathName = "F:\\a.txt";  
  17.       
  18.     public static void main(String[] args) throws IOException {  
  19.         // TODO 自动生成的方法存根  
  20.   
  21.         //generateBigFile();  
  22.         readFile1();  
  23.         readFile2();  
  24.         readFile3();  
  25.     }  
  26.   
  27.     /** 
  28.      * 读大文件 
  29.      * BufferedReader + char[] 
  30.      * @throws IOException 
  31.      */  
  32.     public static void readFile1() throws IOException{  
  33.           
  34.         long start = System.currentTimeMillis();  
  35.         BufferedReader br = new BufferedReader(new FileReader(filePathName));  
  36.           
  37.         char[] buff = new char[1024];  
  38.         int len = -1;  
  39.         while( (len = br.read(buff)) != -1 ){  
  40.             //System.out.print(new String(buff, 0, len));  
  41.         }  
  42.         long end = System.currentTimeMillis();  
  43.         System.out.println("读大文件   BufferedReader + char[], 耗时="+(end-start));    
  44.     }  
  45.   
  46.     /** 
  47.      * 读大文件 
  48.      * FileChannel + ByteBuffer 
  49.      * @throws IOException 
  50.      */  
  51.     private static void readFile2() throws IOException{  
  52.           
  53.         long start = System.currentTimeMillis();  
  54.         FileChannel fc = new FileInputStream(filePathName).getChannel();  
  55.         ByteBuffer buffer = ByteBuffer.allocate(1024);  
  56.           
  57.         while(fc.read(buffer) != -1){  
  58.               
  59.             buffer.flip();  
  60.             //System.out.print(Charset.forName("UTF-8").newDecoder().decode(buffer));;  
  61.             buffer.clear();  
  62.         }  
  63.         long end = System.currentTimeMillis();  
  64.         System.out.println("读大文件  FileChannel + ByteBuffer, 耗时="+(end-start));  
  65.     }  
  66.       
  67.     /** 
  68.      * 读大文件 
  69.      * BufferedReader + CharBuffer 
  70.      * @throws IOException 
  71.      */  
  72.     public static void readFile3() throws IOException{  
  73.           
  74.         long start = System.currentTimeMillis();  
  75.         BufferedReader br = new BufferedReader(new FileReader(filePathName));  
  76.         CharBuffer buff = CharBuffer.allocate(1024);  
  77.         while( br.read(buff) != -1 ){  
  78.             buff.flip();  
  79.             //System.out.print(buff);  
  80.             buff.clear();  
  81.         }  
  82.         long end = System.currentTimeMillis();  
  83.         System.out.println("读大文件   BufferedReader + CharBuffer, 耗时="+(end-start));    
  84.     }  
  85.       
  86. //    public static void readFile4() throws IOException{  
  87. //        
  88. //      long start = System.currentTimeMillis();  
  89. //      FileChannel fc = new FileInputStream(filePathName).getChannel();  
  90. //      int begin = 0, size = 1024;  
  91. //        
  92. //      MappedByteBuffer mappedByteBuffer =   
  93. //         fc.map(FileChannel.MapMode.READ_ONLY, begin, size);  
  94. //        
  95. //      while(mappedByteBuffer.capacity() > 0){  
  96. //        
  97. //          begin += mappedByteBuffer.capacity();  
  98. //          mappedByteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, begin, size);  
  99. //  
  100. //      }  
  101. //      long end = System.currentTimeMillis();  
  102. //      System.out.println("nio读大文件   FileChannel + MappedByteBuffer, 耗时="+(end-start));      
  103. //    }  
  104.       
  105.     /** 
  106.      * 生成一个大文件 a.txt 
  107.      * @throws IOException 
  108.      */  
  109.     private static void generateBigFile() throws IOException{  
  110.           
  111.         long start = System.currentTimeMillis();  
  112.         File bigFile = new File(filePathName);  
  113.         FileWriter fileWriter = new FileWriter(bigFile);  
  114.         for(int i=0;i<100000000;i++){  
  115.             fileWriter.write(Math.random()+"\r\n");  
  116.         }  
  117.         fileWriter.close();  
  118.         long end = System.currentTimeMillis();  
  119.         System.out.println("生成一个大文件 a.txt , 耗时="+(end-start));  
  120.     }  
  121. }  

测试结果:

[plain] view plain copy
  1. 读大文件   BufferedReader + char[], 耗时=5160  
  2. 读大文件   FileChannel + ByteBuffer, 耗时=9091  
  3. 读大文件   BufferedReader + CharBuffer, 耗时=42333  
阅读全文
0 0