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

发布于 2024-07-07 12:27:45 字数 472 浏览 9 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

骷髅 2024-07-14 12:27:46
key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");
key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");
你的呼吸 2024-07-14 12:27:45

Properties 对象可以保存Object,而不仅仅是String。 这往往会被忘记,因为它们绝大多数用于加载 .properties 文件,因此通常只包含 String文档表明调用bundle.getStringArray(key)相当于调用(String[])bundle.getObject (键)。 这就是问题所在:该值不是 String[],而是一个 String

我建议将其存储为逗号分隔的格式,并对该值调用 split()

A Properties object can hold Objects, not just Strings. That tends to be forgotten because they're overwhelmingly used to load .properties files, and so often will only contain Strings. 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.

I'd suggest storing it in comma-delimited format and calling split() on the value.

凝望流年 2024-07-14 12:27:45

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

You can use Commons Configuration, which has methods getList and getStringArray that allow you to retrieve a list of comma separated strings.

少钕鈤記 2024-07-14 12:27:45

嗯,看起来这是一个常见问题,来自线程 此处此处

看起来要么你不使用该方法并自己解析数组的值,要么你编写自己的 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.

风月客 2024-07-14 12:27:45

我不相信从属性文件加载 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.

遇到 2024-07-14 12:27:45

我已经尝试过并且可以找到一种方法。
一种方法是定义ListresourceBundle的子类,然后定义String[]类型的实例变量
并将值分配给键..这是代码

@Override
protected Object[][] getContents() {
    // TODO Auto-generated method stub

    String[] str1 = {"L1","L2"};

    return new Object[][]{

            {"name",str1},
            {"country","UK"}                
    };
}

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

@Override
protected Object[][] getContents() {
    // TODO Auto-generated method stub

    String[] str1 = {"L1","L2"};

    return new Object[][]{

            {"name",str1},
            {"country","UK"}                
    };
}
我的奇迹 2024-07-14 12:27:45

例子:

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

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;
}
标点 2024-07-14 12:27:45

只需使用 spring - Spring .properties 文件:获取元素作为数组

相关代码:

base.module.elementToSearch=1,2,3,4,5,6

@Value("${base.module.elementToSearch}")
  private String[] elementToSearch;

just use spring - Spring .properties file: get element as an Array

relevant code:

base.module.elementToSearch=1,2,3,4,5,6

@Value("${base.module.elementToSearch}")
  private String[] elementToSearch;
梦里兽 2024-07-14 12:27:45
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文