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

Java中使用Properties文件:能否实现属性值的占位符替换?

在Java中用Properties实现变量插值的方案

可以实现,但Java原生Properties类本身不支持${name}这种变量占位符的自动解析,而且你当前的代码存在两个问题:

  • 先调用setProperty("my.name", "John")再执行load(input),加载文件时会覆盖之前设置的my.name属性
  • 代码中prop.getProperty(his.name)存在语法错误,his.name需要用字符串引号包裹

下面提供两种可行的实现方式:

方式一:手动实现变量解析

先加载Properties文件,再设置变量,最后遍历所有属性替换占位符。示例代码如下:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PropertyInterpolator {
    private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{([^}]+)\\}");

    public static void main(String[] args) {
        try (InputStream input = new FileInputStream("path/settings.properties")) {
            Properties prop = new Properties();
            prop.load(input);
            // 设置占位符对应的变量值
            prop.setProperty("name", "John");
            
            // 遍历所有属性,替换其中的占位符
            for (String key : prop.stringPropertyNames()) {
                String value = prop.getProperty(key);
                Matcher matcher = PLACEHOLDER_PATTERN.matcher(value);
                StringBuffer sb = new StringBuffer();
                while (matcher.find()) {
                    String placeholderKey = matcher.group(1);
                    String replacement = prop.getProperty(placeholderKey);
                    // 如果找不到对应变量,保留原占位符
                    matcher.appendReplacement(sb, replacement != null ? replacement : matcher.group(0));
                }
                matcher.appendTail(sb);
                prop.setProperty(key, sb.toString());
            }

            String output = prop.getProperty("his.name");
            System.out.println(output); // 输出:hisNameIsJohn
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方式二:使用第三方库(Apache Commons Configuration)

Apache Commons Configuration库的PropertiesConfiguration原生支持变量插值,无需手动处理逻辑。

  1. 若使用Maven,先添加依赖:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-configuration2</artifactId>
    <version>2.8.0</version>
</dependency>
  1. 示例代码:
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class CommonsConfigExample {
    public static void main(String[] args) {
        try {
            PropertiesConfiguration config = new PropertiesConfiguration("path/settings.properties");
            // 设置占位符对应的变量值
            config.setProperty("name", "John");
            String output = config.getString("his.name");
            System.out.println(output); // 输出:hisNameIsJohn
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 13:05:24