我想将HTML选择框的源存储在配置文件中。这些包含一个很长的字符串,该字符串不会经常更改(但偶尔会更改):
我已经使用了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)
我不想将它们分开放在同一行上,因为,首先,原因是冗长的,其次,有很多原因(> 10)。
我也不想将它们存储在枚举中,因为b/c,如果不重新编译代码就无法更改它们。
关于如何实现这一点的任何提示?
最佳答案
您的示例对我来说很好。如果您使用相同的键指定值列表,则将它们视为列表,并且以下内容应该起作用:
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
在您的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
关于java - 使用相同的键配置属性来创建数组/列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4690817/