如何在Java中加载和解析(ant样式)属性文件?

如何在Java中加载和解析(ant样式)属性文件?,java,properties,ant,Java,Properties,Ant,如何将属性文件加载到Java中的属性对象中,并获得解析的属性值${x}被替换为ant属性?例如,使用此属性文件: foo=1 bar=${foo}.0 我需要将bar属性设置为1.0,而不是${foo}.0。有没有简单的方法 编辑: Alex的解决方案适用于简单场景。就我而言,我必须解决另一个导致这个问题的问题: 加载和分析属性的结果示例代码: import java.util.*; import java.io.*; import org.apache.commons.text.String

如何将属性文件加载到Java中的属性对象中,并获得解析的属性值${x}被替换为ant属性?例如,使用此属性文件:

foo=1
bar=${foo}.0
我需要将bar属性设置为1.0,而不是${foo}.0。有没有简单的方法

编辑: Alex的解决方案适用于简单场景。就我而言,我必须解决另一个导致这个问题的问题:

加载和分析属性的结果示例代码:

import java.util.*;
import java.io.*;
import org.apache.commons.text.StringSubstitutor;

public class Prop {

    Properties          parsedProperties   = null;

    public static Properties parseProperties(String filename) {
        // inner helper class keeping order of properties:
        class LinkedProperties extends Properties {

            private static final long serialVersionUID = 1L;
            private final HashSet<Object> keys = new LinkedHashSet<Object>();

            public LinkedProperties() {
            }

            public Iterable<Object> orderedKeys() {
                return Collections.list(keys());
            }

            public Enumeration<Object> keys() {
                return Collections.<Object>enumeration(keys);
            }

            public Object put(Object key, Object value) {
                keys.add(key);
                return super.put(key, value);
            }
        }

        LinkedProperties result = new LinkedProperties();
        try (InputStream input  = new FileInputStream(filename)) {

            result.load(input);

            @SuppressWarnings("unchecked")
            StringSubstitutor sub = new StringSubstitutor((Map) result);

            for (Object k : result.orderedKeys()) {
                result.setProperty((String)k, sub.replace(result.getProperty((String)k)));
            }
        } catch (IOException ex) { ex.printStackTrace(); }

        return ((Properties)result);
    }

    public static void main(String[] args) {
        Prop app = new Prop();

        // test - write sample properties file:
        try {
            PrintWriter writer = new PrintWriter(new FileWriter("config.properties"));
            writer.println("foo=1");
            writer.println("bar=1.${foo}");
            writer.println("baz=${bar}.0");
            writer.println("xxx=V.${baz}");
            writer.close();
        } catch (IOException ex) { ex.printStackTrace(); }

        // read and parse properties:
        app.parsedProperties = parseProperties("config.properties");

        // debug print:
        for (Object k : app.parsedProperties.keySet()) {
            System.out.println((String)k + " = " + app.parsedProperties.getProperty((String)k));
        }
    }
}
您可以使用Apache Commons文本中的StringSubstitutor,它对Maven的依赖性非常温和~200K:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.8</version>
</dependency>
输出:

resolved: {bar=5.5.0, foo=5.5, baz=5}
您可以使用Apache Commons文本中的StringSubstitutor,它对Maven的依赖性非常温和~200K:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.8</version>
</dependency>
输出:

resolved: {bar=5.5.0, foo=5.5, baz=5}

Spring有这一点,但不确定在不使用大量其他Spring功能依赖性工具的情况下获得它有多容易。Ant有这一点,但同样适用于这里,如何避免从Ant项目等中提取一大堆依赖项?Spring有这一点,尽管不确定在不提取大量其他Spring功能依赖项的情况下获取它有多容易。Ant有这一点,但同样适用于这里,如何避免从Ant项目等中提取所有依赖项?看起来很有希望。它能用于相反的用途吗?我的意思是在加载属性文件的过程中?我不这么认为-您需要提供两个输入字符串,其中变量必须被替换.properties文件的内容和包含加载的属性实例的变量的映射。解析变量后,您必须再次重新加载属性。我认为这会起作用:公共静态属性parsePropertiesProperties orig{properties result=new properties;StringSubstitutor sub=new StringSubstitutorMap orig;for Map.entrySet{result.pute.getKey,sub.replacee.getValue;}返回result;},然后简单地使用如下方法:Properties parsedProp=parsePropertiesprop;看起来很有希望。它能用于相反的用途吗?我的意思是在加载属性文件的过程中?我不这么认为-您需要提供两个输入字符串,其中变量必须被替换.properties文件的内容和包含加载的属性实例的变量的映射。解析变量后,您必须再次重新加载属性。我认为这会起作用:公共静态属性parsePropertiesProperties orig{properties result=new properties;StringSubstitutor sub=new StringSubstitutorMap orig;for Map.entrySet{result.pute.getKey,sub.replacee.getValue;}返回result;},然后简单地使用如下方法:Properties parsedProp=parsePropertiesprop;