如何在java中检查文件的结尾

时间:2016-10-19 09:34:51

标签: java

我有以下代码:

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");
}
}

每次执行此操作时,只执行else部分。 if部分永远不会被执行。

有人可以帮我弄清楚代码中出了什么问题吗?

2 个答案:

答案 0 :(得分:0)

请改用FileReader。您的代码应如下所示:

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示例来自:How to read a large text file line by line using Java?

(为什么要使用这种方法?它应该稍快一些。)

编辑:就像@Andrea发现的那样,当与/结合使用时,请确保您忘记文件夹和文件之间的+

答案 1 :(得分:0)

您的代码似乎没问题。

我执行了上面的代码并且工作正常

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();
        }
    }
}

如果找不到文件,它会引发异常java.io.FileNotFoundException

唯一可疑的是,您的日志文件可能没有行"脚本成功运行"这就是为什么代码没有达到块语句

的原因

我执行了上面的文件代码

cat /tmp/ws/prakash-rcd/labelprakash_label_test.log

开始

脚本成功运行

结束

O / P是

进入循环以检查文件结尾:line:Start

进入循环以检查logs.label update faild

进入循环以检查文件结尾:line:script ran successfully

line found.go out of loop now

以下代码打印"标签更新仅失败一次"

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();
        }
    }
}