java 讀取大數據文件,處理大數據文件性能比較?

通過使用java提供的io,scanner類,apache提供的api處理大文件數據性能分析比較,代碼如下:

  1. package test;  

  2.   

  3. import java.io.BufferedOutputStream;  

  4. import java.io.BufferedReader;  

  5. import java.io.BufferedWriter;  

  6. import java.io.File;  

  7. import java.io.FileInputStream;  

  8. import java.io.FileOutputStream;  

  9. import java.io.FileReader;  

  10. import java.io.IOException;  

  11. import java.io.InputStream;  

  12. import java.io.OutputStream;  

  13. import java.io.OutputStreamWriter;  

  14. import java.io.Reader;  

  15. import java.util.Random;  

  16. import java.util.Scanner;  

  17.   

  18. import org.apache.commons.io.FileUtils;  

  19. import org.apache.commons.io.LineIterator;  

  20. import org.junit.Test;  

  21.   

  22. public class TestFile {  

  23.       

  24.     //@Test  

  25.     //造數據,測試下面各個方法讀取數據性能  

  26.     public void makeFile() throws IOException  

  27.     {  

  28.         File file = new File("D:\\phone.txt");  

  29.           

  30.         OutputStream os = new BufferedOutputStream(new FileOutputStream(file));  

  31.         BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));  

  32.           

  33.         //2百萬  

  34.         for(int i=0; i < 2000000; i++)  

  35.         {  

  36.             bw.write(bulidPhone());  

  37.             bw.newLine();  

  38.         }  

  39.           

  40.         bw.close();  

  41.         os.close();  

  42.     }  

  43.       

  44.     //生成字符串  

  45.     private String bulidPhone()  

  46.     {  

  47.         Long lo = new Random().nextLong();  

  48.         return String.valueOf(lo);  

  49.     }  

  50.       

  51.     /** 

  52.      * @Title: readTxt1 

  53.      * @Description: 使用常規的jdk的io解析輸出文件數據 

  54.      * @throws IOException  

  55.      */   

  56.     @Test  

  57.     public void readTxt1() throws IOException  

  58.     {  

  59.         long start = System.currentTimeMillis();  

  60.         File file = new File("D:\\phone.txt");  

  61.         Reader in = new FileReader(file);  

  62.         BufferedReader br = new BufferedReader(in);  

  63.         while(br.ready())  

  64.         {  

  65.             //System.out.println(br.readLine());  

  66.             br.readLine();  

  67.         }  

  68.           

  69.         in.close();  

  70.         br.close();  

  71.         long end = System.currentTimeMillis();  

  72.         System.out.println("readTxt1方法,使用內存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時間毫秒="+(end-start));  

  73.     }  

  74.       

  75.     /** 

  76.      * @Title: readTxt2 

  77.      * @Description: 使用Scanner掃面文件解析文件數據 

  78.      * @throws IOException  

  79.      */   

  80.     @Test  

  81.     public void readTxt2() throws IOException  

  82.     {  

  83.         long start = System.currentTimeMillis();  

  84.         File file = new File("D:\\phone.txt");  

  85.         InputStream is = new FileInputStream(file);  

  86.         Scanner scan = new Scanner(is,"UTF-8");  

  87.           

  88.         while(scan.hasNextLine())  

  89.         {  

  90.             //System.out.println(scan.nextLine());  

  91.             scan.nextLine();  

  92.             //scan.next();  

  93.         }  

  94.           

  95.         is.close();  

  96.         scan.close();  

  97.           

  98.         long end = System.currentTimeMillis();  

  99.         System.out.println("readTxt2方法,使用內存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時間毫秒="+(end-start));  

  100.     }  

  101.       

  102.     /** 

  103.      * @Title: readTxt3 

  104.      * @Description: 使用org.apache.commons.io.FileUtils,apache工具類解析文件 

  105.      * @throws IOException  

  106.      */   

  107.     @Test  

  108.     public void readTxt3() throws IOException  

  109.     {  

  110.         long start = System.currentTimeMillis();  

  111.         File file = new File("D:\\phone.txt");  

  112.           

  113.         LineIterator it = FileUtils.lineIterator(file, "UTF-8");  

  114.           

  115.         while(it.hasNext())  

  116.         {  

  117.             it.next();  

  118.         }  

  119.           

  120.         it.close();  

  121.           

  122.         long end = System.currentTimeMillis();  

  123.         System.out.println("readTxt3方法,使用內存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時間毫秒="+(end-start));  

  124.     }  

  125. }  


運行結果如下: 
993654e8-739c-3a34-9560-9d252c57371a.png 

通過分析比較: 獲取【下載地址】  
1.apache的api處理時間最短,但是消耗的內存比jdk的io多。 
2.scanner類表現的最差,銷售內存高,時間久。 
3.傳統的jdk的io處理時間稍長,內存消耗低。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章