温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java 读取大数据文件,处理大数据文件性能比较?

发布时间:2020-08-02 23:12:50 来源:网络 阅读:1578 作者:ubvguyt88080 栏目:大数据

通过使用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. }  


运行结果如下: 
java 读取大数据文件,处理大数据文件性能比较? 

通过分析比较: 获取【下载地址】  
1.apache的api处理时间最短,但是消耗的内存比jdk的io多。 
2.scanner类表现的最差,销售内存高,时间久。 
3.传统的jdk的io处理时间稍长,内存消耗低。 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI