我们如何从不同的包中读取多个属性文件

时间:2016-05-22 06:22:37

标签: java properties-file

如何编写java程序来读取不同包中存在的多个属性文件并将它们合并为一个。 我有config.properties文件存在于不同的包示例src / main / resources / folder1,src / main / resources / folder2 ..etc现在我想读取这些属性文件并删除重复项并将它们合并为一个。

3 个答案:

答案 0 :(得分:2)

Properties类扩展Hashtable,因此它可以使用所有Map接口的方法,例如:

Properties p1 = loadProperties("file1.properties");
Properties p2 = loadProperties("file2.properties");

p1.addAll(p2);

答案 1 :(得分:0)

你可以做这样的事情,它为我工作。

 Properties properties = new Properties();

properties.load(new FileInputStream("relative\path\of\config1.properties"));

Properties properties2 = new Properties();
properties2.load(new FileInputStream("relative\path\of\config1.properties"));
.
.
Properties propertiesN = new Properties();
propertiesN.load(new FileInputStream("relative\path\of\configN.properties"));

properties.putAll(properties2);
.
.
properties.putAll(propertiesN);

现在你有一个合并N个属性文件的属性。

答案 2 :(得分:0)

in Spring we have propertyplaceholder you can use it like  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:classpath:source/main/resources/config1/*.properties</value>
            alue>classpath:classpath:source/main/resources/config2/*.properties</value>
            alue>classpath:classpath:source/main/resources/config3/*.properties</value>
        </list>
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
相关问题