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

Java 如何从不同的包中读取多个属性文件,java,properties-file,Java,Properties File,如何编写java程序来读取不同包中的多个属性文件,并将它们合并为一个。 我在不同的包中有config.properties文件,例如src/main/resources/folder1、src/main/resources/folder2……等等。现在我想读取这些属性文件,删除重复的属性文件并将它们合并为一个。该类扩展,因此它可以使用所有映射接口的方法,例如: Properties p1 = loadProperties("file1.properties"); Properties p2 =

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

该类扩展,因此它可以使用所有
映射
接口的方法,例如:

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

p1.addAll(p2);
该类进行了扩展,因此可以使用所有
Map
接口的方法,例如:

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

p1.addAll(p2);

你可以这样做,这对我很有用

 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个属性文件的单一属性。

您可以这样做,这对我很有用

 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);
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>
现在您有了一个合并N个属性文件的单一属性。

在春季,我们有propertyplaceholder,您可以像这样使用它
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>
classpath:classpath:source/main/resources/config1/*.properties value>classpath:classpath:source/main/resources/config2/*.properties value>classpath:classpath:source/main/resources/config3/*.properties
春季我们有propertyplaceholder,您可以像往常一样使用它
classpath:classpath:source/main/resources/config1/*.properties
value>classpath:classpath:source/main/resources/config2/*.properties
value>classpath:classpath:source/main/resources/config3/*.properties

您会提供您现在尝试的代码吗?您会提供您现在尝试的代码吗?有没有通用的方法来读取属性文件,而不是逐个添加所有路径,因为我的根文件夹将是source/main/resources,其中我有多个文件夹使用Spring classpath:classpath:source/main/resources/config1/*.properties value>classpath:classpath:source/main/resources/config2/*.propertiesvalue>classpath:classpath:source/main/resources/config3/*.properties有没有通用的方法来读取属性文件,而不是逐个添加所有路径,因为我的根文件夹将是source/main/resources,其中我有多个文件夹使用Spring classpath:classpath:source/main/resources/config1/*.properties value>classpath:classpath:source/main/resources/config2/*.propertiesvalue>classpath:classpath:source/main/resources/config3/*.properties