0

I have the following code:

String log = "/ws/priyapan-rcd/label" + "priyanka_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");
                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){
    System.out.println("exception occured");
}
}

Everytime I execute this only the else part is executed. The if part never gets executed.

Can somebody help me figuring out what is wrong in my code?

mnille
  • 1,328
  • 4
  • 16
  • 20
noob_coder
  • 749
  • 4
  • 15
  • 35
  • Use `FileReader`, take a look: http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java – Fusseldieb Oct 19 '16 at 09:38
  • 1
    "/ws/priyapan-rcd/label" is right or you are forgetting a final "/"? – Andrea Oct 19 '16 at 09:40
  • 1
    What are the contents of the file? Scanner#hasNextLine will be false when you reach the end of the file. – matt Oct 19 '16 at 10:14
  • If it always goes to the `else` part, it means no line in the file contains the required string. Please show the contents of the file. – RealSkeptic Oct 19 '16 at 10:29

2 Answers2

0

Use FileReader instead. Your code should look something like this:

String log = "/ws/priyapan-rcd/label" + "priyanka_label_test" + ".log";
File file = new File(log);

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
        if (line.contains("script ran successfully")) {
            System.out.println("line found.go out of loop now");
            break;
        }
    }
}
catch (Exception e) {
    System.out.println("come inside loop to check logs.label update faild");
}

FileReader example from: How to read a large text file line by line using Java?

(Why use this method? It should be slightly faster.)

EDIT: And like @Andrea spotted, be sure that you dont forgot a / between folder and file, when combining with a +.

Community
  • 1
  • 1
Fusseldieb
  • 1,324
  • 2
  • 19
  • 44
  • That's strange. Are you sure that your file is in `/ws/priyapan-rcd/label/...`? That seems like an absolute path, and as I know, linux hasn't a 'base' folder called `ws`. – Fusseldieb Oct 19 '16 at 10:25
  • the log file is /ws/priyapan-rcd/labelpriyanka_label_test.log and not /ws/priyapan-rcd/label/priyanka_label_test.log – noob_coder Oct 19 '16 at 10:26
  • @priyanka Please post the **full** location of the script and the file, so that I can take a look. Also please inform if your system is Windows or Linux. – Fusseldieb Oct 19 '16 at 10:26
  • @Fusseldieb..i am not sure if i can post the location here..but i think my script is working fine and all the o/p for the script is getting stored in the log file /ws/priyapan-rcd/label/" + "priyanka_label_test" + ".log" – noob_coder Oct 19 '16 at 10:51
  • as per my code i need to check till the end of the log file – noob_coder Oct 19 '16 at 10:52
  • here are a few lines towards the end of my log file – noob_coder Oct 19 '16 at 10:53
  • commit command ran successsfully Set attribute "DDTS_STATE" to "BID" successfully commit done.remove the views now Label_update.sh script ran successfully – noob_coder Oct 19 '16 at 10:53
  • @Fusseldeib the code that you suggested is working now..it is successfully executing the if part now..but one small problem is the code keeps on checking the log line by line and prints "come inside loop to check logs.label update faild" so many times untill the correct line is found...is there any way so that this code will only print the line once..as in it should print "come inside loop to check logs.label update faild" once it has read all the lines from the log and could not find the required line.can you please suggest something – noob_coder Oct 19 '16 at 11:59
  • @priyanka Just remove the `else` and the `System.out.println` command, as it isn't necessary. – Fusseldieb Oct 19 '16 at 12:18
0

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();
        }
    }
}
Prakash
  • 163
  • 8
  • @Prakash.when i tried it was not working but this code works..but if the log file has many lines so this code reads all the lines one by one and prints the line "come inside loop to check logs.label update faild" so many times.is there a way to read untill the last line but print "come inside loop to check logs.label update faild" only once and not everytime the code reads a line from the log – noob_coder Oct 19 '16 at 12:54
  • @priyanka. outside while set boolean scriptExecutedSuccessfully = false; inside if set scriptExecutedSuccessfully = true; remove else. after while loop if(!scriptExecutedSuccessfully) { System.out.println("label update faild"); } – Prakash Oct 19 '16 at 14:13
  • @prakash...i actually need the else part too – noob_coder Oct 19 '16 at 14:38