如果在文件中找不到关键字“ERROR”,则打印一次“Nothing Found”,此代码扫描每一行并打印每一行的输出。但如果在多行中找到“ERROR”,则需要打印所有“发现错误。
任何帮助将不胜感激!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Scan {
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(new File("Users/home/test.txt"));
while(s.hasNextLine()){
//read the file line by line
String nextLine = s.nextLine();
//check if the next line contains the key word
if(nextLine.contains("ERROR"))
{
//whatever you want to do when the keyword is found in the file
System.out.println("Failed" + " " + nextLine);
}
else if(nextLine.contains("Failed!")){
System.out.println("Not Found");
}
}
}
}