0

i have generated a code for calling an file and it works fine. now i have different names that exists in an file.

the text file look like this

job running fine at 00:05:10 completed successfully at 00:05:11

job running fine at 00:15:06 completed successfully at 00:15:11

for suppose i want to check weather successfully is there or not

if successfully there then it should print successfully word is there otherwise it should print successfully word is not there can any one give some examples how to do code for this

Ram
  • 1
  • 1
  • 1

2 Answers2

1

From what I see, you have a file with multiple lines, and for each line a job recap. So you need to iterate through lines and for each line check if the word "successfully" is present or not. Something like this should work:

BufferedReader br = new BufferedReader(new FileReader(file));  
String line = null; 
while ((line = br.readLine()) != null) {  
   if (line.contains("successfully") {
       System.out.println("Successfully word is there")
   } else {
       System.out.println("Successfully word is NOT there")
   }
} 
0

You can check it like this.

   public static void compareInFile(String inputWord)
    {

    String word = "";

    File file = new File("Readfile.txt"); 
    try
    {
    Scanner input = new Scanner(file);
    while(input.hasNext()) 
    {
        word = input.next();
        if(inputWord.equals(word))
              System.out.println("found successfully");
    }

    }catch(Exception error)
    {
    }
}
Anju
  • 167
  • 12