使用Java读取文件(server.log)时出现问题

时间:2018-12-06 07:48:15

标签: java file

我正在尝试使用一些句子搜索日志文件,然后将其传递到地图,但其无法正常工作。甚至日志中也有“ bow”一词,这说明失败了。

尝试在server.log文件中搜索test_input1, test_input2 ...的值,即使该值存在也无法打印

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    public class Sample {
        public static void main(String[] args) {
            String filePath = "C:\\Users\\xxxxxxxx\\jboss-6.0.0-final\\server\\default\\log\\server.log";
            String test_input1 = "some meaningful log";
            String test_input2 = "bow";
            String test_input3 = "bat";

            Map<String, String> test_results = new HashMap<String, String>();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(filePath));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println("File ---> " + line);

                    if (line.equalsIgnoreCase(test_input1)) {
                        test_results.put("Test Case 1", "passed");
                    } else {
                        test_results.put("Test Case 1", "failed");
                    }

                    if (line.equalsIgnoreCase(test_input2)) {
                        test_results.put("Test Case 2", "passed");
                    } else {
                        test_results.put("Test Case 2", "failed");
                    }

                    if (line.equalsIgnoreCase(test_input3)) {
                        test_results.put("Test Case 3", "passed");
                    } else {
                        test_results.put("Test Case 3", "failed");
                    }
                }

                System.out.println("Summary of test cases: ");

            for (Map.Entry<String, String> entry : test_results.entrySet()) {
                if (entry.getKey().contains("Test Case 1")) {
                    String testresult1 = entry.getValue();
                    System.out.println("Test Case 1 Result ===> " + testresult1);
                } else if (entry.getKey().contains("Test Case 2")) {
                    String testresult2 = entry.getValue();
                    System.out.println("Test Case 2 Result ===> " + testresult2);
                } else if (entry.getKey().contains("Test Case 3")) {
                    String testresult3 = entry.getValue();
                    System.out.println("Test Case 3 Result ===> " + testresult3);
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found in path: " + filePath);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (Exception e) {
                System.err.println("Exception while closing bufferedreader " + e.toString());
            }
        }
    }
}

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

添加到地图中的其他逻辑是每次更改地图中的值。意思是每次您处理这条线时,else都会为地图中的所有键修改地图中的值

Itr 1 T1通行证 t2失败 t3失败

Itr 2 T1失败(由于其他原因) t2失败 t3通过

因此,如果仅存在两次迭代,则最终值将来自itr 2