[英]Counting amount of lines in txt file java file io
while (inputStream.hasNextLine()){
System.out.println(count);
count = count + 1 ;
}
例如,我的文本文件有1000行,每行都有信息。 它似乎是在计算所有数字,而不是每一行。
假设inputStream
是Scanner
,则需要使用InputStream
的数据
while (inputStream.hasNextLine()) {
inputStream.nextLine(); <-- add this
...
循环永远不会结束,因为您不会消耗流。 使用Java 8的替代方法:
try (Stream<String> s = Files.lines(Paths.get(file), UTF_8)) {
count = s.count();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.