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从属性文件中检索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检索它们?
9 个解决方案
#1
32
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 documentation indicates 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对象可以保存对象,而不仅仅是字符串。这往往被遗忘,因为它们绝大多数用于加载.properties文件,因此通常只包含字符串。该文档表明调用bundle.getStringArray(key)等同于调用(String [])bundle.getObject(key)。这就是问题:值不是String [],它是一个String。
I'd suggest storing it in comma-delimited format and calling split()
on the value.
我建议以逗号分隔格式存储它并在值上调用split()。
#2
7
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方法,允许您检索逗号分隔字符串列表。
#3
4
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...
看来要么你不使用这个方法并自己解析一个数组的值,要么你自己编写自己的ResourceBundle实现:(也许有一个apache commons项目...
From the JDK source code, it seems the PropertyResourceBundle does not support it.
从JDK源代码来看,PropertyResourceBundle似乎不支持它。
#4
2
example:
mail.ccEmailAddresses=he@anyserver.at, she@anotherserver.at
..
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;
}
#5
1
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。
#6
1
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;
#7
1
key=value1;value2;value3
String[] toArray = rs.getString("key").split(";");
#8
0
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;
}
#9
0
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"}
};
}
#1
32
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 documentation indicates 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对象可以保存对象,而不仅仅是字符串。这往往被遗忘,因为它们绝大多数用于加载.properties文件,因此通常只包含字符串。该文档表明调用bundle.getStringArray(key)等同于调用(String [])bundle.getObject(key)。这就是问题:值不是String [],它是一个String。
I'd suggest storing it in comma-delimited format and calling split()
on the value.
我建议以逗号分隔格式存储它并在值上调用split()。
#2
7
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方法,允许您检索逗号分隔字符串列表。
#3
4
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...
看来要么你不使用这个方法并自己解析一个数组的值,要么你自己编写自己的ResourceBundle实现:(也许有一个apache commons项目...
From the JDK source code, it seems the PropertyResourceBundle does not support it.
从JDK源代码来看,PropertyResourceBundle似乎不支持它。
#4
2
example:
mail.ccEmailAddresses=he@anyserver.at, she@anotherserver.at
..
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;
}
#5
1
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。
#6
1
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;
#7
1
key=value1;value2;value3
String[] toArray = rs.getString("key").split(";");
#8
0
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;
}
#9
0
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"}
};
}