Java 加载多个属性文件

Java 加载多个属性文件,java,properties-file,Java,Properties File,是否可以在Java中堆叠加载的属性?例如,我可以做: Properties properties = new Properties(); properties.load(new FileInputStream("file1.properties")); properties.load(new FileInputStream("file2.properties")); 和从这两个站点访问属性?您可以执行以下操作: Properties properties = new Properties()

是否可以在Java中堆叠加载的属性?例如,我可以做:

Properties properties = new Properties();

properties.load(new FileInputStream("file1.properties"));
properties.load(new FileInputStream("file2.properties"));
和从这两个站点访问属性?

您可以执行以下操作:

Properties properties = new Properties();

properties.load(new FileInputStream("file1.properties"));

Properties properties2 = new Properties();
properties2.load(new FileInputStream("file2.properties"));

properties.putAll(properties2);

注意:所有维护的密钥都是唯一的。因此,使用相同键加载的后续属性将被覆盖。仅保留以下内容供参考:)

是属性堆栈
Properties
extends
Hashtable
load()
只需对每个键值对调用
put()

相关代码来自:


换句话说,从文件加载不会清除当前条目。但是,请注意,如果两个文件包含具有相同密钥的条目,则第一个文件将被覆盖。

实际上,是的。你可以这样做。如果任何属性重叠,则较新加载的属性将取代较旧的属性。

是的,您需要在构造函数中传递默认属性文件。像这样你可以把他们锁起来

例如:


这也应该起作用。如果在file1.properties和file2.properties中定义了相同的属性,则file2.properties中的属性将生效

    Properties properties = new Properties();
    properties.load(new FileInputStream("file1.properties"));
    properties.load(new FileInputStream("file2.properties"));

现在属性映射将具有来自这两个文件的属性。如果同一个键出现在file1和file2中,file1中的键的值将在属性中用file2中的值进行更新,因为我先调用file1,然后调用file2。

您可以更动态地执行此操作,处理数量不定的文件

此方法的参数应为带有属性文件路径的列表。我将该方法设置为静态,将其放在具有其他消息处理相关函数的类上,并在需要时调用它:

public static Properties loadPropertiesFiles(LinkedList<String> files) {
    try {
        Properties properties = new Properties();

                for(String f:files) {
                    Resource resource = new ClassPathResource( f );
                    Properties tempProp = PropertiesLoaderUtils.loadProperties(resource);
                    properties.putAll(tempProp);
                }
                return properties;
    }
    catch(IOException ioe) {
                return new Properties();
    }
}
公共静态属性加载属性文件(LinkedList文件){
试一试{
属性=新属性();
用于(字符串f:文件){
资源资源=新类路径资源(f);
Properties tempProp=PropertiesLoaderUtils.loadProperties(资源);
properties.putAll(tempProp);
}
归还财产;
}
捕获(ioe异常ioe){
返回新属性();
}
}

如果file2.properties包含与file1.properties中定义的属性同名的属性,则只有file2.properties中这些属性的值才会出现。右。不能同时保留这两个属性,因为键必须是唯一的。建议的方法是在构造函数中传递默认属性文件。属性扩展映射只是一个实现细节。@Puce-
Properties
没有以文件名作为参数的构造函数。你说的是真的,但它没有回答发帖人提出的问题。@tskuzzy的答案实际上是正确的。如果属性有不同的名称,那么答案是肯定的。否,如果属性具有相同的名称。如果属性名称冲突,您必须自己提供堆栈。建议的方法是在构造函数中传递默认属性文件。Properties extends Map只是一个实现细节。这种行为不是由
Properties
契约声明的(换句话说,没有记录使用未记录特性可能产生的所有后果)。这是正确的,应该加以考虑。我只是对实际的结果感兴趣,而不是文档化的行为。”
Properties
extends
java.util.Hashtable
(源代码:)起初我喜欢这个,但现在我有所保留。抛开BufferedInputStream的使用不谈,这怎么会比OP的代码更好呢?在这两种情况下,都是将第二个文件直接加载到包含第一个文件属性的Properties对象中。但在本例中,您正在创建一个新的property对象。好处是什么?如果使用此方法,并且出于某种原因对属性进行迭代,则必须使用
propertyNames()
stringPropertyNames()
来获取要迭代的列表。如果使用基础的
Map
方法,如
entrySet()
keySet()
,则构造函数中指定的属性将不包括在内。
    Properties properties = new Properties();
    properties.load(new FileInputStream("file1.properties"));
    properties.load(new FileInputStream("file2.properties"));
public static Properties loadPropertiesFiles(LinkedList<String> files) {
    try {
        Properties properties = new Properties();

                for(String f:files) {
                    Resource resource = new ClassPathResource( f );
                    Properties tempProp = PropertiesLoaderUtils.loadProperties(resource);
                    properties.putAll(tempProp);
                }
                return properties;
    }
    catch(IOException ioe) {
                return new Properties();
    }
}