统计文件行数,推荐使用LineNumberReader
一、主题:
读取文本文件最大行数性能比较:lineNumberReader > Files.lines
二、code
1 @Test
2 public void testLineReader() throws IOException {
3 File testFile = new File("D:\\temp\\test\\line.txt");
4 if (!testFile.exists()) {
5 testFile.getParentFile().mkdirs();
6 testFile.createNewFile();
7 FileOutputStream fos = new FileOutputStream(testFile, true);
8 long line = 10000000;
9 while (line-- > 0) {
10 fos.write((line + System.getProperty("line.separator")).getBytes());
11 }
12 }
13
14 long startTime = System.currentTimeMillis();
15 long count = 0;
16 for(int i = 0; i<1000; i++)
17 count = Files.lines(Paths.get(testFile.getPath())).count();
18 long endTime = System.currentTimeMillis();
19 System.out.println("files.lines lines:" + count +"spend time:" + (endTime - startTime) + " ms");
20
21 long startTime2 = System.currentTimeMillis();
22 long fileLength = testFile.length();
23 for(int i = 0; i<1000; i++){
24 LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(testFile));
25 lineNumberReader.skip(fileLength);
26 count = lineNumberReader.getLineNumber();
27 lineNumberReader.close();
28 }
29 long endTime2 = System.currentTimeMillis();
30 System.out.println("lines:" + count +"spend time:" + (endTime2 - startTime2) + " ms");
31 }
二、测试结果:
files.lines lines:10000000spend time:333404 ms
lines:10000000spend time:174362 ms
三、结论:
1.使用LineNumberReader统计速度是Files.lines的两倍
2.Files.lines代码比LineNumberReader要简洁许多
统计文件行数,推荐使用LineNumberReader的更多相关文章
- Shell脚本统计文件行数
Shell脚本统计文件行数 转自 http://www.jb51.net/article/61943.htm 示例:row_count.sh文件 awk '{print NR}' row_cou ...
- Linux统计文件行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- wc命令--Linux统计文件行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- Linux - wc统计文件行数、单词数或字节数
一 wc简单介绍 wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, words, and bytes in files).在Windows的 ...
- Linux下wc命令统计文件行数/词数/字符数/最长行字符数
wc命令帮助 $ wc --help Usage: wc [OPTION]... [FILE]... or: wc [OPTION]... --files0-from=F Print newline, ...
- linux 使用wc命令统计文件行数、字数及大小
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- Linux wc -l 统计文件行数存在的问题
1.使用这种方式效率较低,而且不注意可能出现错误 find . -name "*.pc" |xargs wc -l 直接查看 total 不是正确的值. 原因: 这种方式存在一个 ...
- 一个用shell写的统计目录下统计文件行数的代码
此代码是用来回忆当初学的shell脚本,很多知识忘了,写这个花了我三个小时的时间 #!/bin/ksh #count *.c file the num of lineg_csum=0g_hsum=0 ...
- bash 统计文件行数
#假设文件名是:fortest.gtf declare -i fileLinesfileLines=`sed -n '$=' fortest.gtf`echo $fileLines #-------- ...
随机推荐
- python-基础入门-2
这里介绍两个,相当于c中的scanf函数 第一个raw_input 1 age=raw_input("how old are you ") 2 print "you ar ...
- guitar pro 系列教程(十七):Guitar Pro怎么导入音色库?
前面的章节讲述了关于Guitar Pro相关功能的介绍以及使用,其中也有提到音色库,玩音乐的朋友都知道,音色库是一个乐器的必备,今天小编要跟大家讲的就是关于Guitar Pro音色库是如何导入进去的, ...
- php数组学习记录01
array_change_key_case array_change_key_case - 将数组中的所有键名修改为全大写或小写 <?php $input_array = array(" ...
- 完全图的最短Hamilton路径——状压dp
题意:给出一张含有n(n<20)个点的完全图,求从0号节点到第n-1号节点的最短Hamilton路径.Hamilton路径是指不重不漏地经过每一个点的路径. 算法进阶上的一道状压例题,复杂度为O ...
- Codeforces Round #656 (Div. 3) 题解
A. Three Pairwise Maximums #构造 题目链接 题意 给定三个正整数\(x,y,z\),要求找出正整数\(a,b,c\),满足\(x=max(a,b), y=max(a,c), ...
- 做IT需要掌握的电力基础知识
电流 损耗 直流电的传输损耗大,所以不适合长距离传输, 交流电的传输损耗小,所以适合长距离传输, 使用 直流电电压稳定,无白躁声,故适於电子产品使用(例如电视机,收音机电脑等), 交流电要经过整流/开 ...
- centOs7.5.64之前的操作系统搭建GitLab记录
GitLab搭建步骤: 1. Install and configure the necessary dependencies (1)yum install curl openssh-server o ...
- springboot补充
springboot中的日志: 在默认的spring-boot-starter中,会引入spring-boot-starter-logging, 而springboot-starte-longing中 ...
- 20200428_在centos7.2上挂载ntfs和备份文件到移动硬盘
[root@localhost ~]# fdisk -l 磁盘 /dev/sda:2000.4 GB, 2000398934016 字节,3907029168 个扇区 - 设备 Boot Start ...
- 区块链学习7:超级账本项目Fabric中的背书、背书节点、背书策略、背书签名
☞ ░ 前往老猿Python博文目录 ░ 在Hyperledger Fabric区块链中,有背书节点进行背书,Hyperledger Fabric 使用背书策略来定义哪些节点需要执行交易. Hyper ...