Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 860
codingbear Avatar asked Oct 21 '09 17:10

codingbear


2 Answers

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
like image 180
Daniel Rikowski Avatar answered Oct 15 '22 09:10

Daniel Rikowski


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.

like image 36
jitter Avatar answered Oct 15 '22 10:10

jitter