smanzheng 发表于 2015-07-01 13:12

获取上G的文件行数的最快速的代码占用内存少

占用内存少,而且最快的获取海量文件的java代码
原文:http://www.zuidaima.com/share/1550463225072640.htm
代码package com.zuidaima.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
/**
*@author www.zuidaima.com
**/
public class HugeFileLineCounter {

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

    @Rule
    public ContiPerfRule rule = new ContiPerfRule();

    // samples: 20
    // max: 1353
    // average: 1308.55
    // median: 1309
    @Test
    @PerfTest(invocations = 20, threads = 1)
    public void count1() throws IOException {
      LineNumberReader reader = new LineNumberReader(new FileReader(new File(
                hugeFilePath)));
      reader.skip(Long.MAX_VALUE);
      reader.close();
      System.out.println(reader.getLineNumber());
    }

    // most quickly sloution
    // samples: 20
    // max: 654
    // average: 507.6
    // median: 494
    @Test
    @PerfTest(invocations = 20, threads = 1)
    public void count2() throws IOException {
      InputStream is = new BufferedInputStream(new FileInputStream(
                hugeFilePath));
      int count = 0;
      try {
            byte[] c = new byte;
            int readChars = 0;
            while ((readChars = is.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                  if (c == '\n')
                        ++count;
                }
            }
      } finally {
            is.close();
      }
      System.out.println(count);
    }

    // samples: 20
    // max: 622
    // average: 504.8
    // median: 495
    @Test
    @PerfTest(invocations = 20, threads = 1)
    public void count3() throws IOException {
      InputStream is = new BufferedInputStream(new FileInputStream(
                hugeFilePath));
      int count = 0;
      try {
            byte[] c = new byte;
            int readChars = 0;
            while ((readChars = is.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                  if (c == '\n')
                        ++count;
                }
            }
      } finally {
            is.close();
      }
      System.out.println(count);
    }

    // samples: 20
    // max: 1138
    // average: 1050.75
    // median: 1047
    @Test
    @PerfTest(invocations = 20, threads = 1)
    public void count4() throws IOException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
                new FileInputStream(hugeFilePath), "UTF8"));
      int count = 0;
      try {
            char[] c = new char;
            int readChars = 0;
            while ((readChars = reader.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                  if (c == '\n')
                        ++count;
                }
            }
      } finally {
            reader.close();
      }
      System.out.println(count);
    }
}
页: [1]
查看完整版本: 获取上G的文件行数的最快速的代码占用内存少