星空网 > 软件开发 > Java

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

通过使用java提供的io,scanner类,apache提供的api处理大文件数据性能分析比较,代码如下:

package test;  import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.util.Random; import java.util.Scanner;  import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; import org.junit.Test;  public class TestFile {      //@Test   //造数据,测试下面各个方法读取数据性能   public void makeFile() throws IOException   {     File file = new File("D:\\phone.txt");          OutputStream os = new BufferedOutputStream(new FileOutputStream(file));     BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));          //2百万     for(int i=0; i < 2000000; i++)     {       bw.write(bulidPhone());       bw.newLine();     }          bw.close();     os.close();   }      //生成字符串   private String bulidPhone()   {     Long lo = new Random().nextLong();     return String.valueOf(lo);   }      /**   * @Title: readTxt1   * @Description: 使用常规的jdk的io解析输出文件数据   * @throws IOException    */    @Test   public void readTxt1() throws IOException   {     long start = System.currentTimeMillis();     File file = new File("D:\\phone.txt");     Reader in = new FileReader(file);     BufferedReader br = new BufferedReader(in);     while(br.ready())     {       //System.out.println(br.readLine());       br.readLine();     }          in.close();     br.close();     long end = System.currentTimeMillis();     System.out.println("readTxt1方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));   }      /**   * @Title: readTxt2   * @Description: 使用Scanner扫面文件解析文件数据   * @throws IOException    */    @Test   public void readTxt2() throws IOException   {     long start = System.currentTimeMillis();     File file = new File("D:\\phone.txt");     InputStream is = new FileInputStream(file);     Scanner scan = new Scanner(is,"UTF-8");          while(scan.hasNextLine())     {       //System.out.println(scan.nextLine());       scan.nextLine();       //scan.next();     }          is.close();     scan.close();          long end = System.currentTimeMillis();     System.out.println("readTxt2方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));   }      /**   * @Title: readTxt3   * @Description: 使用org.apache.commons.io.FileUtils,apache工具类解析文件   * @throws IOException    */    @Test   public void readTxt3() throws IOException   {     long start = System.currentTimeMillis();     File file = new File("D:\\phone.txt");          LineIterator it = FileUtils.lineIterator(file, "UTF-8");          while(it.hasNext())     {       it.next();     }          it.close();          long end = System.currentTimeMillis();     System.out.println("readTxt3方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));   } } 

运行结果如下: 获取【下载地址】  
java 读取大数据文件,处理大数据文件性能比较? 

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

  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 读取大数据文件,处理大数据文件性能比较?

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外线上营销推广方式:https://www.goluckyvip.com/tag/33430.html
海外销商学院:https://www.goluckyvip.com/tag/33431.html
海外虚拟仓:https://www.goluckyvip.com/tag/33432.html
海外虚拟仓什么意思:https://www.goluckyvip.com/tag/33433.html
海外虚拟仓是什么:https://www.goluckyvip.com/tag/33434.html
海外洋葱仓:https://www.goluckyvip.com/tag/33438.html
仿品独立站从建站、推广、收款到底怎么玩?:https://www.kjdsnews.com/a/1836312.html
仿品独立站从建站、推广、收款到底怎么玩?:https://www.goluckyvip.com/news/186215.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流