Java reads large data files, and compares the performance of processing large data files?

By using the io and scanner classes provided by java, and the api provided by apache to process large file data performance analysis and comparison, the code is as follows:


Java code  
  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.     //Create data and test the performance of the following methods to read data  
  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 million  
  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.     //generate string  
  45.     private String bulidPhone()  
  46.     {  
  47.         Long lo = new Random().nextLong();  
  48.         return String.valueOf(lo);  
  49.     }  
  50.       
  51.     /** 
  52.      * @Title: readTxt1 
  53.      * @Description: Use regular jdk io to parse output file data 
  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. }  


运行结果如下:  
 

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325595676&siteId=291194637