我正在尝试在我的类中创建一个默认的java.util.Properties
对象,并使用它接受的默认属性,并让开发人员通过指定另一个java.util.Properties
对象来覆盖其中的一些属性,但是我找不到一个很好的方法来做到这一点。
目标用途如下:
Properties defaultProperties = new Properties();
defaultProperties.put("key1", "value1");
defaultProperties.put("key2", "value2");
Properties otherProperties = new Properties();
otherProperties.put("key2", "value3");
Properties finalProperties = new Properties(defaultProperties);
//
// I'd expect to have something like:
//
// finalProperties.merge(otherProperties);
//
发布于 2010-01-05 17:24:02
假设您最终想要从文件中读取属性,我将在相同的属性对象中加载这两个文件,如下所示:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("default.properties"));
properties.load(getClass().getResourceAsStream("custom.properties"));
发布于 2010-01-05 16:50:13
是的,您是对的,只需调用putAll方法,就完成了。
发布于 2010-02-04 17:19:26
putAll():将指定映射中的所有映射复制到这个哈希表中。这些映射将替换此哈希表中当前在指定映射中的任何键的任何映射。
Properties merged = new Properties();
merged.putAll(properties1);
merged.putAll(properties2);
第2行完全没有效果。第一个文件中的任何属性都不会出现在合并的属性对象中。
https://stackoverflow.com/questions/2004833
复制相似问题