Java 如何在属性文件中指定值,以便可以使用 ResourceBundle#getStringArray 检索它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/226050/
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
How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?
提问by Grant Wagner
I am trying to use ResourceBundle#getStringArray
to retrieve a String[]
from a properties file. The description of this method in the documentation reads:
我正在尝试使用从属性文件中ResourceBundle#getStringArray
检索 a String[]
。文档中对该方法的描述如下:
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
.
但使用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
?
您如何在属性文件中表示一组键/值对,以便可以使用 检索它们ResourceBundle#getStringArray
?
采纳答案by Robert J. Walker
A Properties
object can hold Object
s, not just String
s. That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only contain String
s. The documentationindicates that calling bundle.getStringArray(key)
is equivalent to calling (String[]) bundle.getObject(key)
. That's the problem: the value isn't a String[]
, it's a String
.
一个Properties
对象可以包含Object
s,而不仅仅是String
s 。这往往会被遗忘,因为它们绝大多数用于加载 .properties 文件,因此通常只包含String
s。文档表明调用bundle.getStringArray(key)
等同于调用(String[]) bundle.getObject(key)
. 这就是问题所在:值不是 a String[]
,而是 a String
。
I'd suggest storing it in comma-delimited format and calling split()
on the value.
我建议以逗号分隔的格式存储它并调用split()
该值。
回答by Chris Kimpton
Umm, looks like this is a common problem, from threads hereand 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...
似乎您不使用该方法并自己解析数组的值,或者您编写自己的 ResourceBundle 实现并自己完成:(。也许有一个 apache commons 项目用于此...
From the JDK source code, it seems the PropertyResourceBundle does not support it.
从 JDK 源代码来看,似乎 PropertyResourceBundle 不支持它。
回答by Alan Krueger
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.
我不相信从属性文件加载 ResourceBundles 是不可能的。PropertyResourceBundle 利用 Properties 类加载属性文件。Properties 类将属性文件作为一组 String->String 映射条目加载,并且不支持提取 String[] 值。
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.
调用 ResourceBundle.getStringArray 只是调用 ResourceBundle.getObject,将结果转换为 String[]。由于 PropertyResourceBundle 只是将它传递给它从文件加载的 Properties 实例,因此您永远无法让它与当前的库存 PropertyResourceBundle 一起使用。
回答by Alan Krueger
public String[] getPropertyStringArray(PropertyResourceBundle bundle, String keyPrefix) {
String[] result;
Enumeration<String> keys = bundle.getKeys();
ArrayList<String> temp = new ArrayList<String>();
for (Enumeration<String> e = keys; keys.hasMoreElements();) {
String key = e.nextElement();
if (key.startsWith(keyPrefix)) {
temp.add(key);
}
}
result = new String[temp.size()];
for (int i = 0; i < temp.size(); i++) {
result[i] = bundle.getString(temp.get(i));
}
return result;
}
回答by Jo?o Silva
You can use Commons Configuration, which has methods getList
and getStringArray
that allow you to retrieve a list of comma separated strings.
您可以使用Commons Configuration,它具有方法getList
并getStringArray
允许您检索逗号分隔的字符串列表。
回答by Murlo
example:
例子:
[email protected], [email protected]
..
..
myBundle=PropertyResourceBundle.getBundle("mailTemplates/bundle-name", _locale);
..
..
public List<String> getCcEmailAddresses()
{
List<String> ccEmailAddresses=new ArrayList<String>();
if(this.myBundle.containsKey("mail.ccEmailAddresses"))
{
ccEmailAddresses.addAll(Arrays.asList(this.template.getString("mail.ccEmailAddresses").split("\s*(,|\s)\s*")));// 1)Zero or more whitespaces (\s*) 2) comma, or whitespace (,|\s) 3) Zero or more whitespaces (\s*)
}
return ccEmailAddresses;
}
回答by Lokesh Garg
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
我已经试过了,可以找到一种方法。一种方法是定义 ListresourceBundle 的子类,然后定义 String[] 类型的实例变量并将值赋给键..这里是代码
@Override
protected Object[][] getContents() {
// TODO Auto-generated method stub
String[] str1 = {"L1","L2"};
return new Object[][]{
{"name",str1},
{"country","UK"}
};
}
回答by chrismarx
just use spring - Spring .properties file: get element as an Array
只需使用 spring - Spring .properties 文件:将元素作为数组获取
relevant code:
相关代码:
base.module.elementToSearch=1,2,3,4,5,6
@Value("${base.module.elementToSearch}")
private String[] elementToSearch;
回答by Sujith
key=value1;value2;value3
String[] toArray = rs.getString("key").split(";");