javaarraysproperties-file

How to store an array in a Java properties file


I'm currently making a .properties file that needs to be loaded and transformed into an array.
There is a possibility of anywhere from 0-25 of each of the property keys to exist.

I tried a few implementations but I'm just stuck at doing this cleanly.
Anyone have any ideas?

foo.1.filename=foo.txt
foo.1.expire=200

foo.2.filename=foo2.txt
foo.2.expire=10

etc more foo's

bar.1.filename=bar.txt
bar.1.expire=100

Where I'll assemble the filename/expire pairings into a data object, as part of an array for each parent property element like foo[myobject]

Formatting of the properties file can change, I'm open to ideas.


Solution

  • Either define a delimiter that will not be a potential value or learn to use XML.

    If you still insist on using properties use one of the methods that will return a list of all keys. Your key appears to have three parts a group identifier (foo, bar) an index (1, 2) and then an element name (filename, expire). Get all the keys break them into their component parts. Create a List for each type of identifier, when processing the list use the identifier to determine which List to add to. Create you paired elements as you said and simply add to the list! If the index order is important either add that as a field to your paired elements or sort the keys before processing.