代码如下:
BufferedReader in= new BufferedReader(new FileReader("C:\\Users\\ASUS\\Desktop\\123.txt"));
Scanner scanner= new Scanner(in);
while((scanner.findInLine("abc"))!=null){
if(scanner.hasNext()){
System.out.println(scanner.next());
}
}
findInLine 只搜索第一行,不搜索其他行。所以它什么都不打印。 我该如何解决这个问题?
最佳答案
您应该遍历所有 行 - 然后如果行匹配,则将其打印出来(或其他)。例如:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// Now check the line, and do whatever you need.
}
或者您仍然可以使用 findInLine
,只需显式调用 nextLine
即可:
while (scanner.hasNextLine()) {
if (scanner.findInLine(...)) {
...
}
// Read to the end of the line whether we found something or not
scanner.nextLine();
}
关于java - findInLine 错误仅搜索第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355520/