如何合并两个 java.util.Properties 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2004833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:23:21  来源:igfitidea点击:

How to merge two java.util.Properties objects?

javaproperties

提问by Igor

I'm trying to have a default java.util.Propertiesobject in my class, with the default properties it accepts, and let the developer override some of them by specifying another java.util.Propertiesobject, but I couldn't find a nice way for doing that.

我试图java.util.Properties在我的类中有一个默认对象,它接受默认属性,并让开发人员通过指定另一个java.util.Properties对象来覆盖其中的一些,但我找不到这样做的好方法。

The intended usage is the following:

预期用途如下:

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);
//

采纳答案by skaffman

java.util.Propertiesimplements the java.util.Mapinterface, and so you canjust treat it as such, and use methods like putAllto add the contents of another Map.

java.util.Properties实现了java.util.Map接口,所以你可以这样对待它,并使用像putAll添加另一个Map.

However, if you treat it like a Map, you need to be very careful with this:

但是,如果您将其视为 Map,则需要非常小心:

new Properties(defaultProperties);

This often catches people out, because it lookslike a copy constructor, but it isn't. If you use that constructor, and then call something like keySet()(inherited from its Hashtablesuperclass), you'll get an empty set, because the Mapmethods of Propertiesdo not take account of the default Propertiesobject that you passed into the constructor. The defaults are only recognised if you use the methods defined in Propertiesitself, such as getPropertyand propertyNames, among others.

这通常会引起人们的注意,因为它看起来像一个复制构造函数,但事实并非如此。如果您使用该构造函数,然后调用类似keySet()(从其Hashtable超类继承)之类的东西,您将得到一个空集,因为 的Map方法Properties不考虑Properties您传递给构造函数的默认对象。仅当您使用Properties自身定义的方法(例如getProperty和 等)时propertyNames,才会识别默认值。

So if you need to merge two Properties objects, it is safer to do this:

所以如果你需要合并两个 Properties 对象,这样做更安全:

Properties merged = new Properties();
merged.putAll(properties1);
merged.putAll(properties2);

This will give you more predictable results, rather than arbitrarily labelling one of them as the "default" property set.

这将为您提供更可预测的结果,而不是随意将其中之一标记为“默认”属性集。

Normally, I would recommend not treating Propertiesas a Map, because that was (in my opinion) an implementation mistake from the early days of Java (Properties should have contained a Hashtable, not extended it - that was lazy design), but the anemic interface defined in Propertiesitself doesn't give us many options.

通常,我建议不要将其Properties视为Map,因为(在我看来)这是 Java 早期的一个实现错误(属性应该包含一个Hashtable,而不是扩展它——这是一种懒惰的设计),但是在Properties本身并没有给我们很多选择。

回答by Jerome

You're almost good:

你几乎很好:

Properties defaultProperties = new Properties();
defaultProperties.setProperty("key1", "value1");
defaultProperties.setProperty("key2", "value2");

Properties finalProperties = new Properties(defaultProperties);
finalProperties.setProperty("key2", "value3");

EDIT:replaced putby setProperty.

编辑:替换putsetProperty.

回答by OscarRyz

Yea you're right just invoke the putAll method and you're done.

是的,您是对的,只需调用 putAll 方法即可完成。

回答by EJB

Assuming you eventually would like to read the properties from a file, I'd go for loading both files in the same properties object like:

假设您最终想从文件中读取属性,我会在同一个属性对象中加载这两个文件,例如:

Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("default.properties"));
properties.load(getClass().getResourceAsStream("custom.properties"));

回答by Volker B.

putAll(): Copies all of the mappings from the specified map to this hashtable. These mappings will replace any mappings that this hashtable had for any of the keys currently in the specified map.

putAll():将所有映射从指定映射复制到此哈希表。这些映射将替换此哈希表对当前在指定映射中的任何键的任何映射。

Properties merged = new Properties();
merged.putAll(properties1);
merged.putAll(properties2);

Line 2 has no effect at all. None of the properties from the first file will be in the merged properties object.

第 2 行根本没有影响。第一个文件中的任何属性都不会在合并的属性对象中。