如何用另一个java.util.Properties中的所有对覆盖java.util.Properties中的某些对?
我正在寻找一个好方法,用另一个Java.util.Properties中的所有对覆盖Java.util.Properties中的Java值? 更美好的事情如下:如何用另一个java.util.Properties中的所有对覆盖java.util.Properties中的某些对?,java,properties,Java,Properties,我正在寻找一个好方法,用另一个Java.util.Properties中的所有对覆盖Java.util.Properties中的Java值? 更美好的事情如下: Properties overrideProperties = createPropertiesFromFile(override); Properties mixProperties = createPropertiesFromFile(basis); for (Entry<Object, Object> entry :
Properties overrideProperties = createPropertiesFromFile(override);
Properties mixProperties = createPropertiesFromFile(basis);
for (Entry<Object, Object> entry : overrideProperties.entrySet()) {
mixProperties.put(entry.getKey(), entry.getValue());
}
Properties overrideProperties=createPropertiesFromFile(override);
Properties mixProperties=createPropertiesFromFile(基础);
for(条目:overrideProperties.entrySet()){
put(entry.getKey(),entry.getValue());
}
扩展,因此您应该能够使用该方法将覆盖属性
的所有条目添加到混合属性
:
Properties overrideProperties = createPropertiesFromFile(override);
Properties mixProperties = createPropertiesFromFile(basis);
mixProperties.putAll(overrideProperties);
这是覆盖属性的另一种方法
Properties properties = new Properties();
properties.load(new FileReader("prop1.properties"));
System.out.println(properties);
properties.load(new FileReader("prop2.properties"));
System.out.println(properties);
如果要仅覆盖已存在的条目,但不添加覆盖中可能存在的新键,请执行以下操作:
overrideProperties.forEach(mixProperties::putIfAbsent);
如果要无条件写入每个重写条目,请使用以下命令:
overrideProperties.forEach(mixProperties::put);
属性
对象也被设计为在构建时链接在一起,因此在第二种情况下(取决于您的应用程序),也可以执行以下操作:
Properties defaults = new Properties();
try (InputStream is = Files.newInputStream(Paths.get("defaults.properties"))) {
properties.load(is);
}
Properties custom = new Properties(defaults);
try (InputStream is = Files.newInputStream(Paths.get("override.properties"))) {
custom.load(is);
}
那么呢?putAll方法需要map作为参数,无属性。@Mark
Properties
实现Map
@Mark更精确地说,Hashtable
实现Map
,因此通过扩展Hashtable
,属性也实现Map
。