免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1012 | 回复: 0

获取上G的文件行数的最快速的代码占用内存少 [复制链接]

论坛徽章:
0
发表于 2015-07-01 13:12 |显示全部楼层
占用内存少,而且最快的获取海量文件的java代码
原文:http://www.zuidaima.com/share/1550463225072640.htm
[Java]代码
  1. package com.zuidaima.test;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.LineNumberReader;

  11. import org.databene.contiperf.PerfTest;
  12. import org.databene.contiperf.junit.ContiPerfRule;
  13. import org.junit.Rule;
  14. import org.junit.Test;
  15. /**
  16. *@author www.zuidaima.com
  17. **/
  18. public class HugeFileLineCounter {

  19.     public static final String hugeFilePath = "c:/test.log";

  20.     @Rule
  21.     public ContiPerfRule rule = new ContiPerfRule();

  22.     // samples: 20
  23.     // max: 1353
  24.     // average: 1308.55
  25.     // median: 1309
  26.     @Test
  27.     @PerfTest(invocations = 20, threads = 1)
  28.     public void count1() throws IOException {
  29.         LineNumberReader reader = new LineNumberReader(new FileReader(new File(
  30.                 hugeFilePath)));
  31.         reader.skip(Long.MAX_VALUE);
  32.         reader.close();
  33.         System.out.println(reader.getLineNumber());
  34.     }

  35.     // most quickly sloution
  36.     // samples: 20
  37.     // max: 654
  38.     // average: 507.6
  39.     // median: 494
  40.     @Test
  41.     @PerfTest(invocations = 20, threads = 1)
  42.     public void count2() throws IOException {
  43.         InputStream is = new BufferedInputStream(new FileInputStream(
  44.                 hugeFilePath));
  45.         int count = 0;
  46.         try {
  47.             byte[] c = new byte[1024];
  48.             int readChars = 0;
  49.             while ((readChars = is.read(c)) != -1) {
  50.                 for (int i = 0; i < readChars; ++i) {
  51.                     if (c[i] == '\n')
  52.                         ++count;
  53.                 }
  54.             }
  55.         } finally {
  56.             is.close();
  57.         }
  58.         System.out.println(count);
  59.     }

  60.     // samples: 20
  61.     // max: 622
  62.     // average: 504.8
  63.     // median: 495
  64.     @Test
  65.     @PerfTest(invocations = 20, threads = 1)
  66.     public void count3() throws IOException {
  67.         InputStream is = new BufferedInputStream(new FileInputStream(
  68.                 hugeFilePath));
  69.         int count = 0;
  70.         try {
  71.             byte[] c = new byte[2048];
  72.             int readChars = 0;
  73.             while ((readChars = is.read(c)) != -1) {
  74.                 for (int i = 0; i < readChars; ++i) {
  75.                     if (c[i] == '\n')
  76.                         ++count;
  77.                 }
  78.             }
  79.         } finally {
  80.             is.close();
  81.         }
  82.         System.out.println(count);
  83.     }

  84.     // samples: 20
  85.     // max: 1138
  86.     // average: 1050.75
  87.     // median: 1047
  88.     @Test
  89.     @PerfTest(invocations = 20, threads = 1)
  90.     public void count4() throws IOException {
  91.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  92.                 new FileInputStream(hugeFilePath), "UTF8"));
  93.         int count = 0;
  94.         try {
  95.             char[] c = new char[1024];
  96.             int readChars = 0;
  97.             while ((readChars = reader.read(c)) != -1) {
  98.                 for (int i = 0; i < readChars; ++i) {
  99.                     if (c[i] == '\n')
  100.                         ++count;
  101.                 }
  102.             }
  103.         } finally {
  104.             reader.close();
  105.         }
  106.         System.out.println(count);
  107.     }
  108. }
复制代码
1.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP