【发布时间】:2025-12-10 05:50:02
【问题描述】:
所以我试图读取一个文本文件并尝试从中只选择正确长度的字符串,但每次我检查长度时,文件扫描器都会跳过一行。有没有办法防止这种/或不同的解决方案?
Scanner fileScanner = new Scanner(fileName);
File file = new File("path" + fileName);
fileScanner = new Scanner(file);
String cache = "";
while (fileScanner.hasNextLine()) {
if (fileScanner.nextLine().length() < 3){
cache = cache + fileScanner.nextLine();
}
}
【问题讨论】:
-
有很多有用的方法你可以试试看是否有效,docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
-
您每行调用两次
nextLine()length < 3。
标签: java text-files java.util.scanner string-length