前后有一个空格 = ...
( 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/