[英]Counting amount of lines in txt file java file io
while (inputStream.hasNextLine()){
System.out.println(count);
count = count + 1 ;
}
For example my text file has 1,000 lines, Each line having information on it. 例如,我的文本文件有1000行,每行都有信息。 It seems to be counting all of the digits and not every line.
它似乎是在计算所有数字,而不是每一行。
Assuming inputStream
is a Scanner
you need to consume the data from the InputStream
假设
inputStream
是Scanner
,则需要使用InputStream
的数据
while (inputStream.hasNextLine()) {
inputStream.nextLine(); <-- add this
...
Your loop never ends because you don't consume the stream. 循环永远不会结束,因为您不会消耗流。 An alternative using Java 8:
使用Java 8的替代方法:
try (Stream<String> s = Files.lines(Paths.get(file), UTF_8)) {
count = s.count();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.