Configuration properties using the same key to create an array / list

I would like to save the source for the html select blocks in the configuration file. They contain long lines that often do not change (but sometimes do):

  • Lorem ipsum sit amet nr. one
  • Lorem ipsum sit amet nr. 2
  • Lorem ipsum sit amet nr. 3
  • Lorem ipsum sit amet nr. 4

I am already using Commons configuration. Is it possible to store them using the same property keys in any configuration object (XMLConfiguration, HierarchicalConfiguration, etc.)? I mean, to get them one way using an interface similar to getStringArray () (or list)? Example:

// reject.reason = Lorem ipsum sit amet nr. 1 // reject.reason = Lorem ipsum sit amet nr. 2 // reject.reason = Lorem ipsum sit amet nr. 3 // reject.reason = Lorem ipsum sit amet nr. 4 config.getStringArray(reject.reason) 

I do not want them to be separated on one line, because, firstly, the reasons are long-lasting, and secondly, there are many reasons (> 10).

I do not want to store them in enumerations, b / c, they cannot be changed without recompiling the code.

Any hints on how to achieve this?

+8
java arrays properties configuration
source share
2 answers

Your example looks good to me. If you specify a list of values ​​using the same key, they will be treated as a list, and the following should work:

 reject.reason = Lorem ipsum sit amet nr. 1 reject.reason = Lorem ipsum sit amet nr. 2 reject.reason = Lorem ipsum sit amet nr. 3 reject.reason = Lorem ipsum sit amet nr. 4 

In Java code:

 PropertiesConfiguration config = new PropertiesConfiguration("gui.properties"); String[] reasons = config.getStringArray("reject.reason"); 

http://commons.apache.org/configuration/userguide/howto_properties.html#Lists_and_arrays

+13
source share

You can save them in a .properties file and as a name ...

 key.0=line0 key.1=line1 key.2=line2 

Then, in your code, iterate over the properties using a for loop looking for "key." + i "key." + i until you get zero.

I have done this in the past to list and configure COM ports, and it works well.

+4
source share

Source: https://habr.com/ru/post/651056/


All Articles