如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们? [英] How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

查看:225
本文介绍了如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ResourceBundle#getStringArray 从属性文件中检索 String [] 。文档中此方法的描述如下:

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

以及逗号分隔的列表:

key=value1,value2,value3

但这些都不能使用 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?

推荐答案

A 属性 object可以包含 Object s ,而不仅仅是 String s。这往往被遗忘,因为它们绝大多数用于加载.properties文件,因此通常只包含 String s。 文档表明调用 bundle.getStringArray(key)相当于调用(String [])bundle.getObject(key)。这就是问题所在:值不是 String [] ,它是 String

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.

我建议以逗号分隔的格式存储它,并在值上调用 split()

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

这篇关于如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆