java.util.Properties 中的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1432481/
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-12 12:01:44  来源:igfitidea点击:

Multiple values in java.util.Properties

javaproperties

提问by ClueMinus

It seems that java.util.Properties assumes one value per propery key. That is,

似乎 java.util.Properties 假定每个属性键一个值。那是,

foo=1
foo=2

is not expected,

不是预期的,

Is there a class for this kind of multi-value property sheet, which also provides the load method?

这种多值属性表有没有类,还提供了load方法?

采纳答案by Nick Holt

Try:

尝试:

foo=1,2

String[] foos = properties.getProperty("foo").split(",");

回答by ZZ Coder

The java.util.Properties function is pretty limited. If you want support list, you might want try PropertyConfiguration from Apache Commons Configuration,

java.util.Properties 函数非常有限。如果您需要支持列表,您可能需要尝试来自 Apache Commons Configuration 的 PropertyConfiguration,

http://commons.apache.org/configuration/userguide/howto_properties.html#Using_PropertiesConfiguration

http://commons.apache.org/configuration/userguide/howto_properties.html#Using_PropertiesConfiguration

With it, you can set any delimiters to your list and it will split for you automatically. You can also do other fancy things in properties file. For example,

有了它,您可以为您的列表设置任何分隔符,它会自动为您拆分。您还可以在属性文件中做其他花哨的事情。例如,

foo=item1, item2
bar=${foo}, item3
number=123

You can retrieve it like this,

你可以像这样检索它,

Configuration config = new PropertiesConfiguration("your.properties");
String[] items = config.getStringArray("bar"); // return {"item1", "item2", "item3"}
int number = config.getInt("number", 456); // 456 is default value

回答by KLE

Correct answer by Nick.

尼克的正确答案。

Or, if you can give a different subname to each value, you could have your properties be:

或者,如果您可以为每个值指定不同的子名称,您的属性可以是:

    my.properties

    foo.title=Foo
    foo.description=This a big fat foo.

回答by Pablojim

This won't provide the load method but a place to store them you could use a apache commons multivaluemap:

这不会提供加载方法,而是提供一个存储它们的地方,您可以使用 apache commons 多值映射:

"A MultiValueMap decorates another map, allowing it to have more than one value for a key. "

“MultiValueMap 装饰另一张地图,允许它为一个键拥有多个值。”

This is often a requirement for http request parameters...

这通常是对http请求参数的要求...

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiValueMap.html

回答by Clerenz

If you have a more complex example you might use the following:

如果您有更复杂的示例,您可以使用以下内容:

# pairs of properties
source1=foo
target1=bar
source2=anotherFoo
target2=regardingBar
source3= ...

In your code you will have to search:

在您的代码中,您必须搜索:

Map<String, String> myMap = new HashMap<>();
for (int i=1; i<max; i++) {
  String source = properties.get("source" + i);
  String target = properties.get("target" + i);
  if (source == null || target == null) {
    break;
  }
  myMap.put(source, target);
}

Drawback: updating the properties file. If you remove values *2, all the following values will not be added. To improve you might want to replace the break with a continue and stick to a maximum of allowed pairs.

缺点:更新属性文件。如果删除值 *2,则不会添加以下所有值。为了改进,您可能需要用继续替换中断并坚持最多允许的对。