ファイルにキーワード「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");
}
}
}
}