use*_*894 2 java logging parsing tokenize
所有,
我有一个包含以下内容的日志文件.
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] ****************************
Run Code Online (Sandbox Code Playgroud)
我需要创建两个日志文件,一个用于存储所有请求详细信息,另一个用于存储所有响应详细信息.我该如何解析这个并准备两个日志文件?
我需要以下答案.
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
Run Code Online (Sandbox Code Playgroud)
此致,Kanagaraj
我将如何做到这一点:
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...
}
}
}
Run Code Online (Sandbox Code Playgroud)
给出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] ****************************
Run Code Online (Sandbox Code Playgroud)
它生成,请求.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] ****************************
Run Code Online (Sandbox Code Playgroud)
...和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
Run Code Online (Sandbox Code Playgroud)