gpt4 book ai didi

java - 用 Java 解析日志文件

转载 作者:行者123 更新时间:2023-12-02 06:21:41 26 4
gpt4 key购买 nike

全部,

我有一个包含以下内容的日志文件。

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

问候,卡纳加拉吉

最佳答案

这是我的做法:

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

关于java - 用 Java 解析日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4489330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com