所有
我有一个包含以下内容的日志文件。
Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222
Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
我需要创建两个日志文件,一个用于存储所有请求详细信息,另一个用于存储所有响应详细信息。我该如何解析这个并准备两个日志文件?
我需要以下答案。
Log 1:
Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222
2010-12-14 12:42:13.724 [ 6796] ****************************
Log 2:
Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
此致 Kanagaraj
答案 0 :(得分:9)
我将如何做到这一点:
import java.io.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
try {
PrintWriter requests = new PrintWriter("requests.txt");
PrintWriter responses = new PrintWriter("responses.txt");
PrintWriter currentLog = null;
Scanner s = new Scanner(new File("log.txt"));
while (s.hasNextLine()) {
String line = s.nextLine();
if (line.startsWith("Request from"))
currentLog = requests;
else if (line.startsWith("Response from"))
currentLog = responses;
else if (currentLog != null)
currentLog.println(line);
}
requests.close();
responses.close();
s.close();
} catch (IOException ioex) {
// handle exception...
}
}
}
鉴于 log.txt
Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222
Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
它生成 requests.txt
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222
2010-12-14 12:42:13.724 [ 6796] ****************************
...和 responses.txt
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960] ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960] tttttttttttttttttttttttttttt
答案 1 :(得分:0)
您可以使用正则表达式或RandomAccessFiles以您希望的方式获取数据。
答案 2 :(得分:0)
StringBuilder requestBuilder = new StringBuilder("Request from Centercord.\n");
StringBuilder responseBuilder = new StringBuilder("Response from Centercord.\n");
boolean isResponse = false;
for (String line:getLinesFromLogFile()) {
if (line.startsWith("Response")) {
if(isResponse)
responseBuilder.append("\n");
isResponse = true;
} else if (line.startsWith("Request")) {
if(!isResponse)
requestBuilder.append("\n");
isResponse = false;
} else {
if (isResponse) {
responseBuilder.append(line).append("\n");
} else {
requestBuilder.append(line).append("\n");
}
}
}
现在将两个StringBuilders的内容转储到目标文件。
答案 3 :(得分:0)
要减少样板代码,请下载Google Guava并使用以下代码:
File inputFile = new File("input.log");
File outputLog1 = new File("log1.log");
File outputLog2 = new File("log2.log");
String match1 = "[ 6796]";
String match2 = "[ 5960]";
Files.append("Request from Centercord.\n", outputLog1, Charsets.UTF_8);
Files.append("Response from Centercord.\n", outputLog2, Charsets.UTF_8);
List<String> lines = Files.readLines(inputFile, Charsets.UTF_8);
for (String line : lines) {
if(line.contains(match1)) Files.append(line + "\n", outputLog1, Charsets.UTF_8);
if(line.contains(match2)) Files.append(line + "\n", outputLog2, Charsets.UTF_8);
}