gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:20:26 25 4
gpt4 key购买 nike

前后有一个空格 = ...

( 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com