简体   繁体   English

计算文本文件中的行数(java)

[英]counting the number of lines in a text file (java)

Below is how i count the number of lines in a text file.下面是我如何计算文本文件中的行数。 Just wondering is there any other methods of doing this?只是想知道有没有其他方法可以做到这一点?

while(inputFile.hasNext()) {    
    a++;
    inputFile.nextLine();
}
inputFile.close();

I'm trying to input data into an array, i don't want to read the text file twice.我正在尝试将数据输入到数组中,我不想读取文本文件两次。

any help/suggestions is appreciated.任何帮助/建议表示赞赏。

thanks谢谢

If you are using java 7 or higher version you can directly read all the lines to a List using readAllLines method.如果您使用的是 java 7 或更高版本,您可以使用 readAllLines 方法直接将所有行读取到列表中。 That would be easy那会很容易

readAllLines 读取所有行

List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());

Then the size of the list will return you number of lines in the file然后列表的大小将返回文件中的行数

int noOfLines = lines.size();

If you are using Java 8 you can use streams :如果您使用的是 Java 8,则可以使用流:

long count = Files.lines(Paths.get(filename)).count();

This will have good performances and is really expressive.这将有很好的表现并且非常富有表现力。

The downside (compared to Thusitha Thilina Dayaratn answer) is that you only have the line count.缺点(与Tsuchitha Thilina Dayaratn 的答案相比)是您只有行数。 If you also want to have the lines in a List, you can do (still using Java 8 streams) :如果您还想在列表中包含这些行,您可以这样做(仍然使用 Java 8 流):

// First, read the lines
List<String> lines = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Then get the line count
long count = lines.size();

If you just want to add the data to an array, then I append the new values to an array.如果您只想将数据添加到数组中,那么我会将新值附加到数组中。 If the amount of data you are reading isn't large and you don't need to do it often that should be fine.如果您正在阅读的数据量不大并且您不需要经常这样做,那应该没问题。 I use something like this, as given in this answer: Reading a plain text file in Java我使用这样的东西,如这个答案中给出的: Reading a plain text file in Java

BufferedReader fileReader = new BufferedReader(new FileReader("path/to/file.txt"));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
} finally {
    br.close();
}

If you are reading in numbers, the strings can be converted to numbers, say for integers intValue = Integer.parseInt(text)如果您正在阅读数字,则字符串可以转换为数字,比如整数 intValue = Integer.parseInt(text)

I do not have enough reputation to comment but @superbob answer is almost perfect, indeed you must ensure to pass Charset.defaultCharset() as 2nd parameter like :我没有足够的声誉来发表评论,但@superbob 的回答几乎是完美的,确实您必须确保将 Charset.defaultCharset() 作为第二个参数传递,例如:

Files.lines(file.toPath(), Charset.defaultCharset()).count()

That's because Files.lines used UTF-8 by default and then using as it is on non default UTF-8 system can produce java.nio.charset.MalformedInputException.这是因为 Files.lines 默认使用 UTF-8,然后在非默认 UTF-8 系统上使用它会产生 java.nio.charset.MalformedInputException。

You can do it just like this, everyone has some complicated way of doing it when you can just simply do it like this:你可以这样做,每个人都有一些复杂的方法,当你可以简单地这样做时:

$file = "file.txt";
$count = count(file($file));  
echo "there are $count lines";

Very easy to use.非常容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM