java 使用相同键创建数组/列表的配置属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4690817/
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
Configuration properties using the same key to create an array / list
提问by Mike Minicki
I would like to store the source for html select boxes in a configuration file. These contain a lengthy strings that don't change often (but occassionaly do):
我想将 html 选择框的源代码存储在配置文件中。这些包含不经常更改的冗长字符串(但偶尔会更改):
- Lorem ipsum sit amet nr. 1
- Lorem ipsum sit amet nr. 2
- Lorem ipsum sit amet nr. 3
- Lorem ipsum sit amet nr. 4
- Lorem ipsum 坐 amet nr。1
- Lorem ipsum 坐 amet nr。2
- Lorem ipsum 坐 amet nr。3
- Lorem ipsum 坐 amet nr。4
I already use commons-configuration. Is it possible to store them using the same property keys in some kind of configuration object (XMLConfiguration, HierarchicalConfiguration, etc.)? I mean to be able to retrieve them in one go using interface similar to getStringArray()(or list)? Example:
我已经在使用 commons-configuration。是否可以在某种配置对象(XMLConfiguration、HierarchicalConfiguration 等)中使用相同的属性键来存储它们?我的意思是能够使用类似于getStringArray()(或列表)的接口一次性检索它们?例子:
// reject.reason = Lorem ipsum sit amet nr. 1
// reject.reason = Lorem ipsum sit amet nr. 2
// reject.reason = Lorem ipsum sit amet nr. 3
// reject.reason = Lorem ipsum sit amet nr. 4
config.getStringArray(reject.reason)
I don't want to keep them separated on the same line because, first, the reasons are lengthy, and second, there are lots of reasons (> 10).
我不想将它们放在同一行上,因为首先,原因很长,其次,有很多原因(> 10)。
I don't want to store them in enums either, b/c it will be impossible to change them without recompiling the code.
我也不想将它们存储在枚举中,b/c 如果不重新编译代码就不可能更改它们。
Any hints on how to achieve this?
关于如何实现这一目标的任何提示?
回答by dogbane
Your example looks fine to me. If you specify a list of values using the same key, they are treated as a list, and the following should work:
你的例子对我来说很好。如果您使用相同的键指定一个值列表,它们将被视为一个列表,并且以下内容应该有效:
reject.reason = Lorem ipsum sit amet nr. 1
reject.reason = Lorem ipsum sit amet nr. 2
reject.reason = Lorem ipsum sit amet nr. 3
reject.reason = Lorem ipsum sit amet nr. 4
In your Java code:
在您的 Java 代码中:
PropertiesConfiguration config = new PropertiesConfiguration("gui.properties");
String[] reasons = config.getStringArray("reject.reason");
http://commons.apache.org/configuration/userguide/howto_properties.html#Lists_and_arrays
http://commons.apache.org/configuration/userguide/howto_properties.html#Lists_and_arrays
回答by Romain Hippeau
You could store them in a .properties file and name as ...
您可以将它们存储在 .properties 文件中并命名为 ...
key.0=line0
key.1=line1
key.2=line2
Then in your code iterate through properties with a for loop looking for "key." + i
until you get a null back.
然后在您的代码中使用 for 循环遍历属性,"key." + i
直到返回空值。
I have done this in the past to enumerate and configure com ports and it works well.
我过去曾这样做过以枚举和配置 com 端口,并且效果很好。