如何在固定位置替换文本字段:基于数据库查询的字符串字段动态更新

本文介绍如何读取固定格式的文本文件,根据指定位置提取字段、查询数据库获取新值,并仅对满足条件(如字段值为“120”)的行,在精确字符偏移处安全替换目标子串,最终生成更新后的文件。

本文介绍如何读取固定格式的文本文件,根据指定位置提取字段、查询数据库获取新值,并仅对满足条件(如字段值为“120”)的行,在精确字符偏移处安全替换目标子串,最终生成更新后的文件。

在处理银行对账单、金融报文或传统主机导出的定长格式文件(Fixed-Width Format)时,常需按列位置(而非分隔符)解析并修改特定字段。本教程以一个典型场景为例:输入文件中每行是长度固定的字符串,需识别 section 字段(位于索引 11–13,共3位),当其值为 "120" 时,提取 OldValue(索引 47–66,共20位),查询数据库获得对应 NewValue,并原位替换该20字符区域——关键在于精准覆盖、不破坏格式对齐

✅ 核心实现步骤

  1. 逐行读取并校验长度:避免 String.substring() 抛出 IndexOutOfBoundsException;
  2. 按索引提取字段:使用 substring(start, end)(注意:end 为exclusive);
  3. 条件过滤与数据库查询:仅对 section.equals("120") 的行执行 DB 查询;
  4. 精准字符串替换不可用 replace(oldValue, newValue)(会全局匹配、破坏格式),而应使用字符串拼接重建:
    String updatedLine = line.substring(0, 47) + 
                         padRight(newValue, 20) +  // 确保长度恒为20(见下方说明)
                         line.substring(67);
  5. 写回文件:使用 Files.write() 原子覆盖,或写入临时文件后重命名。

? 完整可运行示例(含数据库查询模拟)

import java.io.*;
import java.nio.file.*;
import java.sql.*;
import java.util.*;

public class FixedWidthReplacer {
    private static final String DB_URL = "jdbc:h2:mem:testdb"; // 示例:H2内存库,实际请替换为您的DB
    private static final String DB_USER = "";
    private static final String DB_PASS = "";

    public static void main(String[] args) {
        Path inputFile = Paths.get("C:/Users/20230309.in");
        Path outputFile = Paths.get("C:/Users/20230309.out");

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS)) {
            initDatabase(conn); // 初始化测试数据(实际项目中省略)

            List<String> lines = Files.readAllLines(inputFile, StandardCharsets.UTF_8);
            List<String> updatedLines = new ArrayList<>();

            for (String line : lines) {
                // 安全校验:确保行足够长(至少67字符)
                if (line.length() < 67) {
                    updatedLines.add(line);
                    continue;
                }

                // 提取 section (pos 11-13 → substring(11,14))
                String section = line.substring(11, 14).trim();
                if ("120".equals(section)) {
                    String oldValue = line.substring(47, 67).trim(); // 提取 OldValue (47-66)
                    String newValue = queryNewValue(conn, oldValue);
                    // 精准覆盖:保持总长不变,右补空格对齐
                    String paddedNewValue = padRight(newValue, 20);
                    String updatedLine = line.substring(0, 47) + paddedNewValue + line.substring(67);
                    updatedLines.add(updatedLine);
                } else {
                    updatedLines.add(line); // 不符合条件,保持原样
                }
            }

            Files.write(outputFile, updatedLines, StandardCharsets.UTF_8);
            System.out.println("✅ 文件已更新,共处理 " + lines.size() + " 行。");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String queryNewValue(Connection conn, String oldValue) throws SQLException {
        String sql = "SELECT NewValue FROM myTable WHERE OldValue = ?";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, oldValue);
            try (ResultSet rs = ps.executeQuery()) {
                return rs.next() ? rs.getString("NewValue") : oldValue; // 未查到则保留原值
            }
        }
    }

    // 确保字符串长度为 targetLen,不足则右补空格(维持定长格式)
    private static String padRight(String s, int targetLen) {
        if (s == null) s = "";
        return String.format("%-" + targetLen + "s", s);
    }

    // 【测试用】初始化 H2 内存表(实际项目中删除此方法)
    private static void initDatabase(Connection conn) throws SQLException {
        try (Statement stmt = conn.createStatement()) {
            stmt.execute("CREATE TABLE myTable (OldValue VARCHAR(20), NewValue VARCHAR(20))");
            stmt.execute("INSERT INTO myTable VALUES ('13626142', 'NewValue1'), ('13626143', 'NewValue2')");
        }
    }
}

⚠️ 关键注意事项

通过以上结构化处理,您即可可靠、高效地完成定长文本中基于数据库驱动的字段精准替换,兼顾可读性、健壮性与生产可用性。

本文转载于:互联网 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。