简体   繁体   English

如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们?

[英]How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

I am trying to use ResourceBundle#getStringArray to retrieve a String[] from a properties file. 我试图使用ResourceBundle#getStringArray从属性文件中检索String[] 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 . 但这些都不能使用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检索它们?

A Properties object can hold Object s , not just String s. Properties对象可以保存Object ,而不仅仅是String That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only contain String s. 这往往被遗忘,因为它们绝大多数用于加载.properties文件,因此通常只包含String The documentation indicates that calling bundle.getStringArray(key) is equivalent to calling (String[]) bundle.getObject(key) . 该文档表明调用bundle.getStringArray(key)等同于调用(String[]) bundle.getObject(key) That's the problem: the value isn't a String[] , it's a String . 这就是问题:值不是String[] ,它是一个String

I'd suggest storing it in comma-delimited format and calling split() on the value. 我建议以逗号分隔格式存储它并在值上调用split()

您可以使用Commons Configuration ,它具有getListgetStringArray方法,允许您检索逗号分隔字符串列表。

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似乎不支持它。

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;
}

I don't believe this is possible with ResourceBundles loaded from a properties file. 我不相信从属性文件加载ResourceBundles可以实现这一点。 The PropertyResourceBundle leverages the Properties class to load the properties file. PropertyResourceBundle利用Properties类来加载属性文件。 The Properties class loads a properties file as a set of String->String map entries and doesn't support pulling out String[] values. Properties类将属性文件作为一组String-> String映射条目加载,并且不支持拉出String []值。

Calling ResourceBundle.getStringArray just calls ResourceBundle.getObject, casting the result to a String[]. 调用ResourceBundle.getStringArray只调用ResourceBundle.getObject,将结果转换为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. 由于PropertyResourceBundle只是将其移交给从文件加载的Properties实例,因此您永远无法使用当前的库存PropertyResourceBundle。

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;
key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");
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;
}

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"}                
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用变量访问 getStringArray - How do I access getStringArray with a variable Java:如何在运行时将数据库中的属性添加到R​​esourceBundle? - Java: How do I add properties from a database to a ResourceBundle on runtime? 通过覆盖ResourceBundle重用Properties文件中的键值 - Reusing key values in Properties file by overriding ResourceBundle Java:如何将图像从文件添加到ResourceBundle? - Java: How can I add images from a file into a ResourceBundle? 在Java中使用ResourceBundle类时,我们可以使用属性文件指定目录路径吗? - Can we specify directory path with the property file while using ResourceBundle class in Java? 在Javascript中使用Resourcebundle属性 - Using Resourcebundle Properties in Javascript 如何在使用ResourceBundle API获取的属性文件中转义花括号 - How to escape curly braces in my properties file which is fetched using the ResourceBundle API 使用ResourceBundle将属性文件从一种语言翻译成另一种语言 - Translate the Properties File from one language to another using ResourceBundle 当我使用ResourceBundle从属性文件中获取值时,出现异常 - When I am fetching the values from property file using ResourceBundle,I get an exception 如何在Wicket中访问中央ResourceBundle - How can I access an central ResourceBundle in Wicket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM