[英]Problem when reading file (server.log) with java
我正在嘗試使用一些句子搜索日志文件,然后將其傳遞給地圖,但無法正常工作。 即使在日志中也有單詞“ bow”,它也表明失敗。
嘗試在server.log文件中搜索test_input1, test_input2 ...
的值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());
}
}
}
}
預先感謝您的幫助
您添加到地圖中的其他邏輯是每次更改地圖中的值。 意思是每次處理該行時,else都會修改地圖中所有鍵的地圖中值
Itr 1 T1通過t2失敗t3失敗
Itr 2 T1失敗(由於其他原因)t2失敗t3通過
因此,如果僅存在兩次迭代,則最終值將來自itr 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.