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

如何使用java.util.Properties从配置文件获取多值sourceSystem数组(不依赖Ini库)

问题原因
  • Properties的load()方法处理重复键时,会直接覆盖之前的值,最终只会保留最后一个(也就是RTY),所以用getProperty()只能拿到单个值,无法获取所有sourceSystem的内容。
  • 现有代码还写错了键名:应该是sourceSystem,不是sourceSystemMUB。
解决方案

自己读取配置文件的每一行,手动解析并收集重复键的所有值,代码示例:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class IniParser {
    public static void main(String[] args) {
        List<String> sourceSystemValues = new ArrayList<>();
        String line;
        
        try (BufferedReader reader = new BufferedReader(new FileReader("fileProcessing.ini"))) {
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                // 跳过空行和注释行(如果存在)
                if (line.isEmpty() || line.startsWith("#")) {
                    continue;
                }
                // 解析键值对
                int equalsIndex = line.indexOf('=');
                if (equalsIndex > 0) {
                    String key = line.substring(0, equalsIndex).trim();
                    String value = line.substring(equalsIndex + 1).trim();
                    if ("sourceSystem".equals(key)) {
                        sourceSystemValues.add(value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 转换为目标数组
        String[] source_system_1 = sourceSystemValues.toArray(new String[0]);
    }
}
代码说明
  • 用BufferedReader逐行读取文件,避开Properties自动覆盖重复键的机制。
  • 手动解析每一行的键值对,判断键为sourceSystem时,将对应值加入集合。
  • 最后把集合转成数组,就能得到预期的{"ABC", "XYZ", "FGH", "RTY"}。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 18:27:24