java - 如何在文本文件中搜索字符串备份 = True

标签 java

前后有一个空格 = ...

( Backup = True )------ 是要搜索的字符串(= 之间有空格)

File file = new File(
                "D:\\Users\\kbaswa\\Desktop\\New folder\\MAINTENANCE-20150708.log.txt");

        Scanner scanner = null;
        try {
            scanner = new Scanner(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // now read the file line by line...
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.next();
            lineNum++;
            String name="Backup = True";
            if (line.contains(name)) {
                System.out.println("I found "+name+ " in file " +file.getName());
                   break;
            }
            else{
                System.out.println("I didnt found it");
            }

        }

    }

最佳答案

Scanner.next() 返回下一个完整标记,因此它将返回类似 Backup 的内容,然后下次返回 =循环,然后下次true

使用Scanner.nextLine()一次性获取整行。

关于java - 如何在文本文件中搜索字符串备份 = True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31309082/

相关文章:

java - 重新访问 View 时清除 session 作用域 bean 的数据

java - 将本体显示为树

java - 如何从Jform调用另一个线程运行方法

java - 为什么 ClassNotFoundException 会表现出这种行为?

java - 如何从这个循环中提取反射?

java - SWT:如何找到SWT.RADIO风格的按钮?

java - 线程 "main"java.awt.AWTError : BoxLayout can't be shared 中的异常

java - 使用 CellSignalStrengthGsm 只给出 Integer.MAX_VALUE

java - 服务器返回 HTTP 响应代码 : 500 for URL

java - 单击按钮时 setText 不会更改或更新