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

后端 未结 9 1438
失恋的感觉
失恋的感觉 2020-12-03 06:56

I am trying to use ResourceBundle#getStringArray to retrieve a String[] from a properties file. The description of this method in the documentation

相关标签:
9条回答
  • 2020-12-03 07:27

    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.

    0 讨论(0)
  • 2020-12-03 07:28

    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.

    0 讨论(0)
  • 2020-12-03 07:31

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

    0 讨论(0)
提交回复
热议问题