Search code examples
javaini4j

Java ini4j - reading multiple options from .ini file


I'm trying to read multiple values using ini4j, the docs say it should be possible with the Options class.

Here is my sample .ini file (./dwarfs.ini)

[dopey]
age = 23
fortuneNumber = 11
fortuneNumber = 33
fortuneNumber = 55

here is the code to read it:

Ini ini = new Ini();
Config conf = new Config();
conf.setMultiOption(true);
ini.setConfig(conf);
ini.load(new FileReader("./dwarfs.ini"));

but fortuneNumber property is just 55 after reading and I'd want it to be an array or a list, anything.


Solution

  • The web presence for the ini4j project contains (among others) very simple tutorials. One of these tutorials explains, how to retrieve multiple values. How could you expect to get a list or an array, when using a fetch method that returns a single reference? Look at the API!

    In the tutorial, there is a part explaining multi values:

    [ini4j] library introduces MultiMap interface, which is extends normal Map, but allows multiply values per keys. You can simply index values for a given key, similar to indexed properties in JavaBeans api.

    There is also an example:

    String n1 = sneezy.get("fortuneNumber", 0);  // = 11
    String n2 = sneezy.get("fortuneNumber", 1);  // = 22
    String n3 = sneezy.get("fortuneNumber", 2);  // = 33
    String n4 = sneezy.get("fortuneNumber", 3);  // = 44
    

    In this example, sneezy is a Section, but it should also work with an Ini.

    And just to make it complete: An Ini also knows a method List<V> getAll(Object key).