Your code seems fine.
I have executed above code and its working fine
import java.io.File;
import java.util.Scanner;
public class ReadFileScanner
{
public static void main(String s[])
{
String log = "/tmp/ws/prakash-rcd/label" + "prakash_label_test" + ".log";
File file = new File(log);
String line = null;
try
{
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine())
{
line = scanner.nextLine();
System.out.println("come inside loop to check end of file: line: " + line);
if (line.contains("script ran successfully"))
{
System.out.println("line found.go out of loop now");
break;
}
else
{
System.out.println("come inside loop to check logs.label update faild");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
It raises exception java.io.FileNotFoundException in case of file not found
The only suspect is, your log file may not have line "script ran successfully" that's why code is not reaching if block statements
I executed the above code for file
cat /tmp/ws/prakash-rcd/labelprakash_label_test.log
Start
script ran successfully
End
And O/P is
come inside loop to check end of file: line: Start
come inside loop to check logs.label update faild
come inside loop to check end of file: line: script ran successfully
line found.go out of loop now
Below code prints "label update failed only once"
import java.io.File;
import java.util.Scanner;
/**
*
* @author root
*/
public class ReadFileScanner
{
public static void main(String s[])
{
String log = "/tmp/ws/prakash-rcd/label" + "prakash_label_test" + ".log";
File file = new File(log);
String line = null;
boolean logFailureMsg = true;
try
{
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine())
{
line = scanner.nextLine();
System.out.println("come inside loop to check end of file: line: " + line);
if (line.contains("script ran successfully"))
{
System.out.println("line found.go out of loop now");
break;
}
else
{
if(logFailureMsg)
{
System.out.println("label update faild");
logFailureMsg = false;
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}