首页>代码>获取上G的文件行数的最快速的java代码>/filelinecounter/src/main/java/com/test/HugeFileLineCounter.java
package com.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;

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[1024];
			int readChars = 0;
			while ((readChars = is.read(c)) != -1) {
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\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[2048];
			int readChars = 0;
			while ((readChars = is.read(c)) != -1) {
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\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[1024];
			int readChars = 0;
			while ((readChars = reader.read(c)) != -1) {
				for (int i = 0; i < readChars; ++i) {
					if (c[i] == '\n')
						++count;
				}
			}
		} finally {
			reader.close();
		}
		System.out.println(count);
	}
}
最近下载更多
xiaoxiaokaixinmeng  LV1 2021年9月27日
blazing  LV2 2019年1月10日
街角de風鈴o  LV1 2018年4月23日
dswxpn  LV1 2018年1月26日
流水浅浅  LV1 2017年12月20日
1054261752  LV8 2017年8月23日
scpcyzxb  LV16 2017年8月14日
1185250941  LV2 2017年8月8日
hero5876  LV12 2017年5月26日
81194882  LV4 2017年4月13日
最近浏览更多
woldxy  LV12 4月1日
crosa_Don  LV18 2022年6月14日
single815  LV1 2021年5月29日
xiaoxiaokaixinmeng  LV1 2021年5月18日
廖业贵  LV18 2021年2月28日
xiongfan  LV6 2021年2月18日
dongzhan  LV12 2020年12月7日
uglyee  LV2 2020年9月28日
dapeng0011  LV13 2020年5月11日
cclovecoding 2020年4月29日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友