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

如何用Java读取文件并按sender id拆分提取End-End Delay值

Java按Sender ID拆分End-End Delay值到独立文件

需求回顾

你有一个格式紧凑的文本文件,每条记录以End-End Delay:数值结尾,需要用Java读取该文件,按sender id拆分为4个独立文件,每个文件仅存储对应sender的End-End Delay值,且你没有正则表达式使用经验。

解决方案思路

别担心,我一步步帮你搞定,哪怕没接触过正则也能看懂。我们分三步完成:

  • 读取整个文件内容,把每条完整的记录拆分出来(每条记录都以End-End Delay:开头的结尾部分结束,用这个做拆分标记)
  • 对每条记录,提取出sender id和对应的End-End Delay数值
  • 按sender id将数值写入对应的文件(比如sender_0.txt、sender_1.txt等)

完整代码示例(带详细注释)

代码里的正则部分我会单独解释,保证你能理解:

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DelaySplitter {
    public static void main(String[] args) {
        // 替换成你自己的输入文件路径
        String inputFilePath = "your_input_file.txt";
        // 输出文件的前缀,最终会生成sender_0.txt、sender_1.txt这类文件
        String outputFilePrefix = "sender_";

        try {
            // 1. 读取整个文件的内容
            StringBuilder contentBuilder = new StringBuilder();
            // try-with-resources语法:自动关闭文件流,不用手动处理
            try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    contentBuilder.append(line);
                }
            }
            String fullContent = contentBuilder.toString();

            // 2. 拆分每条记录:用正向预查确保拆分后每条记录都保留完整的End-End Delay部分
            String[] records = fullContent.split("(?=End-End Delay:)");

            // 3. 定义正则表达式,专门提取我们需要的信息
            // 匹配sender id:捕获"sender id: "后面的数字
            Pattern senderPattern = Pattern.compile("sender id: (\\d+)");
            // 匹配延迟值:捕获"End-End Delay:"后面的小数
            Pattern delayPattern = Pattern.compile("End-End Delay:(\\d+\\.\\d+)");

            // 4. 遍历每条记录,提取信息并写入对应文件
            for (String record : records) {
                // 提取sender id
                Matcher senderMatcher = senderPattern.matcher(record);
                String senderId = null;
                if (senderMatcher.find()) {
                    senderId = senderMatcher.group(1); // group(1)就是我们捕获到的数字部分
                }

                // 提取延迟值
                Matcher delayMatcher = delayPattern.matcher(record);
                String delayValue = null;
                if (delayMatcher.find()) {
                    delayValue = delayMatcher.group(1);
                }

                // 如果两个值都提取到了,就写入对应文件
                if (senderId != null && delayValue != null) {
                    String outputFilePath = outputFilePrefix + senderId + ".txt";
                    // 用append模式(true),避免每次写入覆盖之前的内容
                    try (FileWriter fw = new FileWriter(outputFilePath, true)) {
                        fw.write(delayValue + System.lineSeparator()); // 每个值单独占一行
                    }
                }
            }

            System.out.println("拆分完成!已生成对应sender的延迟文件");

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("处理文件时出现错误:" + e.getMessage());
        }
    }
}

关键部分解释

  • 正则表达式说明:
    • sender id: (\\d+):(\\d+)是捕获组,专门抓sender id后面的数字,\\d代表数字,+表示至少有一个数字
    • End-End Delay:(\\d+\\.\\d+):(\\d+\\.\\d+)抓延迟的小数数值,\\.是转义后的小数点(正则里.是特殊字符,必须转义)
  • 记录拆分技巧:split("(?=End-End Delay:)")里的(?=...)是正向预查,意思是“找到后面跟着End-End Delay:的位置”,这样拆分后每条记录都会保留完整的延迟信息,不会丢失关键内容
  • 资源管理:全程用try-with-resources,Java会自动关闭文件流,避免忘记关流导致的资源泄漏

使用步骤

  1. 把代码里的your_input_file.txt替换成你的实际文件路径
  2. 编译并运行这个Java程序
  3. 程序会在当前目录下生成四个文件,每个文件里是对应sender的所有End-End Delay值,每行一个

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:15:42