计算txt文件java文件io中的行数

计算txt文件java文件io中的行数,java,io,Java,Io,例如,我的文本文件有1000行,每行都有相关信息。它似乎在计算所有的数字,而不是每一行。假设inputStream是扫描仪,您需要使用inputStream while (inputStream.hasNextLine()){ System.out.println(count); count = count + 1 ; } while(inputStream.hasNextLine()){ inputStream.nextLine();假设inputStream是Scanner

例如,我的文本文件有1000行,每行都有相关信息。它似乎在计算所有的数字,而不是每一行。

假设
inputStream
扫描仪,您需要使用
inputStream

while (inputStream.hasNextLine()){
    System.out.println(count);
    count = count + 1 ;
}
while(inputStream.hasNextLine()){

inputStream.nextLine();假设
inputStream
Scanner
您需要使用
inputStream

while (inputStream.hasNextLine()){
    System.out.println(count);
    count = count + 1 ;
}
while(inputStream.hasNextLine()){

inputStream.nextLine();您的循环永远不会结束,因为您不使用流。使用Java 8的另一种选择是:

while (inputStream.hasNextLine()) {
   inputStream.nextLine(); <-- add this
   ...
try(streams=Files.line(path.get(file),UTF_8)){
count=s.count();
}

您的循环永远不会结束,因为您不使用流。使用Java 8的另一种选择是:

while (inputStream.hasNextLine()) {
   inputStream.nextLine(); <-- add this
   ...
try(streams=文件.line(path.get(file),UTF\u 8)){
count=s.count();
}