我们如何确定文本文件中的行数? [英] How do we determine the number of lines in a text file?

查看:135
本文介绍了我们如何确定文本文件中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有一个本地文件,如下所示:

Hi all I have a local file which looks like this:

AAA   Anaa
AAC   EL-ARISH
AAE   Annaba 
AAF   APALACHICOLA MUNI AIRPORT
AAG   ARAPOTI
AAL   Aalborg Airport
AAM   Mala Mala
AAN   Al Ain 
AAQ   Anapa
AAR   Aarhus Tirstrup Airport
AAT   Altay
AAX   Araxa
AAY   Al Ghaydah
...

Java教程建议通过 java.io.File.length
并将结果除以50。

Java Tutorials suggests estimating the number of lines in a file by doing java.io.File.length and dividing the result by 50.

但是没有更稳固的方法来获得行数在一个文本文件中(但无需支付阅读整个文件的开销le)?

But isn't there a more "solid" way to get the number of lines in a text file (yet without having to pay for the overhead of reading the entire file)?

推荐答案

你得到的估算算法的好处是非常快速:一个 stat(2)调用然后一些除法。无论文件有多大或多小,它都需要相同的时间和内存。但是在大量输入上也是非常错误的。

The benefit to the estimation algorithm you've got is that it is very fast: one stat(2) call and then some division. It'll take the same length of time and memory no matter how large or small the file is. But it's also vastly wrong on a huge number of inputs.

获得特定数字的最佳方法可能是实际读取整个文件,寻找'\ n'字符。如果你用大的二进制块读取文件(想想16384字节或更大的2的幂)并查找你感兴趣的特定字节,它可以接近磁盘IO带宽。

Probably the best way to get the specific number is to actually read through the entire file looking for '\n' characters. If you read the file in in large binary blocks (think 16384 bytes or a larger power of two) and look for the specific byte you're interested in, it can go at something approaching the disk IO bandwidth.

这篇关于我们如何确定文本文件中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆