java - findInLine 错误仅搜索第一行

标签 java

代码如下:

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/

相关文章:

java - 将主方法输入的值传递给私有(private)变量

java - 设置Hibernate中表的存储引擎

java - 什么哈希函数更好?

java - 当给卡高程 ="0dp"时,CardBackgroundColor 无法工作

java - 使用 Java 发送 Tor NEWNYM 请求

java - 带有 Java 7 的 Mac OSX 上的透明 JFrame/JWindow

java - 为什么下面的代码返回设置大小为 5?即使 hashcode 和 equals 方法被重写,它也应该返回集合大小为 3

java - 如果您指定的最大堆大小大于可用 RAM,会发生什么情况

java - 在 arraylist 中缓存 iterable 以在 reducer 中迭代两次不起作用

java - Play 中 Controller 操作之间的事务