You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java如何按指定规则解析日志文件并转换输出为CSV文件

Java日志解析转CSV实现方案

需求概述

通过Java程序读取本地.log格式日志文件,按照指定规则解析字段后,输出为标准CSV文件。

规则与样例

  • 待解析日志文件样例
2022-06-01 11:00:00 wt.nm=aa&wt.ti=t1&
2022-06-02 12:00:00 wt.nm=ab&wt.ti=t2&
2022-06-03 10:00:00 wt.nm=ac&wt.ti=t3&
  • 字段解析规则
    • 每行开头的日期、时间字段以空格分隔
    • name字段提取wt.nm=后对应的取值
    • title字段提取wt.ti=后对应的取值
    • 日志中所有键值对以&作为结尾分隔符
  • 目标CSV输出格式样例
date,time,name,title 
2022-06-01,11:00:00,aa,t1
2022-06-02,12:00:00,ab,t2
2022-06-03,10:00:00,ac,t3

初始待补全代码

现有代码仅实现了基础的文件读写,未加入解析逻辑,也未处理流资源释放、表头写入等问题:

import java.io.*;
public class test {
    public static void main(String[] args) {
        try{
            BufferedReader in = new BufferedReader(new FileReader("/Users/ts/Desktop/test/src/0606.log"));
            FileWriter wb = new FileWriter("/Users/ts/Desktop/testcsv.csv");
            String str;
            while((str=in.readLine()) != null) {
                System.out.println(str);
                wb.write(str);
            }
        }
        catch (IOException e) {
        }
    }
}

补全后可运行代码

核心补全点:加入逐行字段解析逻辑、写入CSV表头、用try-with-resources自动释放IO资源、补全异常打印:

import java.io.*;

public class LogToCsv {
    public static void main(String[] args) {
        // 替换为实际的输入日志路径、输出CSV路径
        String inputLogPath = "/Users/ts/Desktop/test/src/0606.log";
        String outputCsvPath = "/Users/ts/Desktop/testcsv.csv";

        // try-with-resources 自动关闭IO流,避免资源泄漏
        try (
            BufferedReader reader = new BufferedReader(new FileReader(inputLogPath));
            FileWriter writer = new FileWriter(outputCsvPath)
        ) {
            // 写入CSV表头
            writer.write("date,time,name,title\n");
            String line;
            // 逐行读取日志
            while ((line = reader.readLine()) != null) {
                // 跳过空行
                if (line.trim().isEmpty()) {
                    continue;
                }
                // 拆分日期、时间、参数段:按空格拆分为3部分
                String[] lineParts = line.split(" ", 3);
                String date = lineParts[0];
                String time = lineParts[1];
                String paramSegment = lineParts[2];

                String name = "";
                String title = "";
                // 按&拆分键值对
                String[] kvPairs = paramSegment.split("&");
                for (String kv : kvPairs) {
                    if (kv.startsWith("wt.nm=")) {
                        name = kv.substring("wt.nm=".length());
                    } else if (kv.startsWith("wt.ti=")) {
                        title = kv.substring("wt.ti=".length());
                    }
                }

                // 拼接CSV行写入,追加换行符
                String csvLine = String.join(",", date, time, name, title) + "\n";
                writer.write(csvLine);
                System.out.print("解析完成行:" + csvLine);
            }
            System.out.println("日志解析转CSV完成,文件输出路径:" + outputCsvPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

代码说明:如果后续日志新增其他键值对字段,只需要在遍历kvPairs的逻辑中新增对应字段的取值判断即可,扩展性较好。

内容的提问来源于stack exchange,提问作者Lyra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 18:42:25