溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

java 讀取大數(shù)據(jù)文件,處理大數(shù)據(jù)文件性能比較?

發(fā)布時(shí)間:2020-08-02 23:12:50 來(lái)源:網(wǎng)絡(luò) 閱讀:1592 作者:ubvguyt88080 欄目:大數(shù)據(jù)

通過(guò)使用java提供的io,scanner類(lèi),apache提供的api處理大文件數(shù)據(jù)性能分析比較,代碼如下:

  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.     //造數(shù)據(jù),測(cè)試下面各個(gè)方法讀取數(shù)據(jù)性能  

  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百萬(wàn)  

  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: 使用常規(guī)的jdk的io解析輸出文件數(shù)據(jù) 

  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方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));  

  73.     }  

  74.       

  75.     /** 

  76.      * @Title: readTxt2 

  77.      * @Description: 使用Scanner掃面文件解析文件數(shù)據(jù) 

  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方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));  

  100.     }  

  101.       

  102.     /** 

  103.      * @Title: readTxt3 

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

  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方法,使用內(nèi)存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用時(shí)間毫秒="+(end-start));  

  124.     }  

  125. }  


運(yùn)行結(jié)果如下: 
java 讀取大數(shù)據(jù)文件,處理大數(shù)據(jù)文件性能比較? 

通過(guò)分析比較: 獲取【下載地址】  
1.apache的api處理時(shí)間最短,但是消耗的內(nèi)存比jdk的io多。 
2.scanner類(lèi)表現(xiàn)的最差,銷(xiāo)售內(nèi)存高,時(shí)間久。 
3.傳統(tǒng)的jdk的io處理時(shí)間稍長(zhǎng),內(nèi)存消耗低。 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI