ini4j - How to get all the key names in a setting?

17.6k views Asked by At

I've decided to use ini file to store simple key-value pair configuration for my Java application.

I googled and searched stackoverflow and found that ini4j is highly recommended for parsing and interpreting ini files in Java. I spent some time reading the tutorial on ini4j site; however, I was not sure how to get all the key values for a setting in an ini file.

For instance, if I have a ini file like this:

[ food ]
name=steak
type=american
price=20.00

[ school ]
dept=cse
year=2
major=computer_science

and assume that I do not know names of keys ahead of time. How do I get the list of keys so that I can eventually retrieve the values according to keys? For instance, I would get an array or some kind of data structure that contains 'name', 'type', and 'price' if I get a list of keys for food.

Can someone show me an example where you would open an ini file, parse or interpret it so that an app knows all the structure and values of the ini file, and get the list of keys and values?

4

There are 4 answers

1
jitter On BEST ANSWER

No guarantees on this one. Made it up in 5min. But it reads the ini you provided without further knowledge of the ini itself (beside the knowledge that it consists of a number of sections each with a number of options.

Guess you will have to figure out the rest yourself.

import org.ini4j.Ini;
import org.ini4j.Profile.Section;
import java.io.FileReader;

public class Test {
    public static void main(String[] args) throws Exception {
        Ini ini = new Ini(new FileReader("test.ini"));
        System.out.println("Number of sections: "+ini.size()+"\n");
        for (String sectionName: ini.keySet()) {
            System.out.println("["+sectionName+"]");
            Section section = ini.get(sectionName);
            for (String optionKey: section.keySet()) {
                System.out.println("\t"+optionKey+"="+section.get(optionKey));
            }
        }
    }
}

Check out ini4j Samples and ini4j Tutorials too. As often a not very well documented library.

0
Miguel Sanchez On

In Kotlin:

        val ini = Wini(File(iniPath))
        Timber.e("Read value:${ini}")
        println("Number of sections: "+ini.size+"\n");
        for (sectionName in ini.keys) {
            println("[$sectionName]")
            val section: Profile.Section? = ini[sectionName]
            if (section != null) {
                for (optionKey in section.keys) {
                    println("\t" + optionKey + "=" + section[optionKey])
                }
            }
        }
0
Daniel Rikowski On

I couldn't find anything in the tutorials so I stepped through the source, until I found the entrySet method. With that you can do this:

Wini ini = new Wini(new File(...));
Set<Entry<String, Section>> sections = ini.entrySet(); /* !!! */

for (Entry<String, Section> e : sections) {
    Section section = e.getValue();
    System.out.println("[" + section.getName() + "]");

    Set<Entry<String, String>> values = section.entrySet(); /* !!! */
    for (Entry<String, String> e2 : values) {
        System.out.println(e2.getKey() + " = " + e2.getValue());
    }
}

This code essentially re-prints the .ini file to the console. Your sample file would produce this output: (the order may vary)

[food]
name = steak
type = american
price = 20.00
[school]
dept = cse
year = 2
major = computer_science
0
Richard On

The methods of interest are get() and keySet()

Wini myIni = new Wini (new File ("test.ini"));

// list section names
for (String sName : myIni.keySet()) {
    System.out.println(sName);
}

// check for a section, section name is case sensitive
boolean haveFoodParameters = myIni.keySet().contains("food");

// list name value pairs within a specific section
for (String name : myIni.get("food").keySet() {
    System.out.println (name + " = " + myIni.get("food", name)
}