如何在属性文件中指定值,以便可以使用 ResourceBundle#getStringArray 检索它们?
我正在尝试使用 ResourceBundle#getStringArray
从属性文件中检索 String[]
。 文档中对该方法的描述如下:
从此资源包或其父资源包之一获取给定键的字符串数组。
但是,我尝试将属性文件中的值存储为多个单独的键/值对:
key=value1
key=value2
key=value3
和逗号分隔的列表:
key=value1,value2,value3
但这些都无法使用 ResourceBundle#getStringArray
检索。
如何在属性文件中表示一组键/值对,以便可以使用 ResourceBundle#getStringArray 检索它们?
I am trying to use ResourceBundle#getStringArray
to retrieve a String[]
from a properties file. The description of this method in the documentation reads:
Gets a string array for the given key from this resource bundle or one of its parents.
However, I have attempted to store the values in the properties file as multiple individual key/value pairs:
key=value1
key=value2
key=value3
and as a comma-delimited list:
key=value1,value2,value3
but neither of these is retrievable using ResourceBundle#getStringArray
.
How do you represent a set of key/value pairs in a properties file such that they can be retrieved using ResourceBundle#getStringArray
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Properties
对象可以保存Object
,而不仅仅是String
。 这往往会被忘记,因为它们绝大多数用于加载 .properties 文件,因此通常只包含String
。 文档表明调用bundle.getStringArray(key)
相当于调用(String[])bundle.getObject (键)
。 这就是问题所在:该值不是String[]
,而是一个String
。我建议将其存储为逗号分隔的格式,并对该值调用
split()
。A
Properties
object can holdObject
s, not justString
s. That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only containString
s. The documentation indicates that callingbundle.getStringArray(key)
is equivalent to calling(String[]) bundle.getObject(key)
. That's the problem: the value isn't aString[]
, it's aString
.I'd suggest storing it in comma-delimited format and calling
split()
on the value.您可以使用 Commons Configuration,它具有方法
getList
和getStringArray
允许您检索逗号分隔字符串的列表。You can use Commons Configuration, which has methods
getList
andgetStringArray
that allow you to retrieve a list of comma separated strings.嗯,看起来这是一个常见问题,来自线程 此处和此处。
看起来要么你不使用该方法并自己解析数组的值,要么你编写自己的 ResourceBundle 实现并自己完成:(。也许有一个 apache commons 项目用于此...
从 JDK 源代码,看来 PropertyResourceBundle 不支持它。
Umm, looks like this is a common problem, from threads here and here.
It seems either you don't use the method and parse the value for an array yourself or you write your own ResourceBundle implementation and do it yourself :(. Maybe there is an apache commons project for this...
From the JDK source code, it seems the PropertyResourceBundle does not support it.
我不相信从属性文件加载 ResourceBundles 是不可能的。 PropertyResourceBundle 利用 Properties 类来加载属性文件。 Properties 类将属性文件加载为一组 String->String 映射条目,并且不支持提取 String[] 值。
调用 ResourceBundle.getStringArray 只是调用 ResourceBundle.getObject,将结果转换为 String[]。 由于 PropertyResourceBundle 只是将其交给从文件加载的 Properties 实例,因此您永远无法使其与当前的、库存的 PropertyResourceBundle 一起使用。
I don't believe this is possible with ResourceBundles loaded from a properties file. The PropertyResourceBundle leverages the Properties class to load the properties file. The Properties class loads a properties file as a set of String->String map entries and doesn't support pulling out String[] values.
Calling ResourceBundle.getStringArray just calls ResourceBundle.getObject, casting the result to a String[]. Since the PropertyResourceBundle just hands this off to the Properties instance it loaded from the file, you'll never be able to get this to work with the current, stock PropertyResourceBundle.
我已经尝试过并且可以找到一种方法。
一种方法是定义ListresourceBundle的子类,然后定义String[]类型的实例变量
并将值分配给键..这是代码
I have tried this and could find a way.
One way is to define a subclass of ListresourceBundle, then define instance variable of type String[]
and assign the value to the key.. here is the code
例子:
..
..
example:
..
..
只需使用 spring - Spring .properties 文件:获取元素作为数组
相关代码:
just use spring - Spring .properties file: get element as an Array
relevant code: