千家信息网

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

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,通过使用java提供的io,scanner类,apache提供的api处理大文件数据性能分析比较,代码如下:package test;import java.io.BufferedOutputStre
千家信息网最后更新 2025年02月04日java 读取大数据文件,处理大数据文件性能比较?

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

  1. package test;

  2. import java.io.BufferedOutputStream;

  3. import java.io.BufferedReader;

  4. import java.io.BufferedWriter;

  5. import java.io.File;

  6. import java.io.FileInputStream;

  7. import java.io.FileOutputStream;

  8. import java.io.FileReader;

  9. import java.io.IOException;

  10. import java.io.InputStream;

  11. import java.io.OutputStream;

  12. import java.io.OutputStreamWriter;

  13. import java.io.Reader;

  14. import java.util.Random;

  15. import java.util.Scanner;

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

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

  18. import org.junit.Test;

  19. public class TestFile {

  20. //@Test

  21. //造数据,测试下面各个方法读取数据性能

  22. public void makeFile() throws IOException

  23. {

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

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

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

  27. //2百万

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

  29. {

  30. bw.write(bulidPhone());

  31. bw.newLine();

  32. }

  33. bw.close();

  34. os.close();

  35. }

  36. //生成字符串

  37. private String bulidPhone()

  38. {

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

  40. return String.valueOf(lo);

  41. }

  42. /**

  43. * @Title: readTxt1

  44. * @Description: 使用常规的jdk的io解析输出文件数据

  45. * @throws IOException

  46. */

  47. @Test

  48. public void readTxt1() throws IOException

  49. {

  50. long start = System.currentTimeMillis();

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

  52. Reader in = new FileReader(file);

  53. BufferedReader br = new BufferedReader(in);

  54. while(br.ready())

  55. {

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

  57. br.readLine();

  58. }

  59. in.close();

  60. br.close();

  61. long end = System.currentTimeMillis();

  62. System.out.println("readTxt1方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));

  63. }

  64. /**

  65. * @Title: readTxt2

  66. * @Description: 使用Scanner扫面文件解析文件数据

  67. * @throws IOException

  68. */

  69. @Test

  70. public void readTxt2() throws IOException

  71. {

  72. long start = System.currentTimeMillis();

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

  74. InputStream is = new FileInputStream(file);

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

  76. while(scan.hasNextLine())

  77. {

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

  79. scan.nextLine();

  80. //scan.next();

  81. }

  82. is.close();

  83. scan.close();

  84. long end = System.currentTimeMillis();

  85. System.out.println("readTxt2方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));

  86. }

  87. /**

  88. * @Title: readTxt3

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

  90. * @throws IOException

  91. */

  92. @Test

  93. public void readTxt3() throws IOException

  94. {

  95. long start = System.currentTimeMillis();

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

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

  98. while(it.hasNext())

  99. {

  100. it.next();

  101. }

  102. it.close();

  103. long end = System.currentTimeMillis();

  104. System.out.println("readTxt3方法,使用内存="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())+",使用时间毫秒="+(end-start));

  105. }

  106. }


运行结果如下:


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

0