Preferences Tutorial

The purpose of this document is to familiarize the reader with the usage of the [ini4j] library's Preferences interface. Each chapter contains all the necessary code portions and explanation for a given function.

Code sniplets in this tutorial tested with the following .ini file: dwarfs.ini

As soon as the Preferences object is created it functions as a standard Preferences node, and should be used as such. Implicitly only new nodes can be created in the root node (these will be the sections). In the first level nodes (sections) only values can be created (these will be the options).

In the case of an invalid operation an UnsupportedOperationException type runtime exception is generated. This happens if we try to set a value at the root node or to create a node on the second level, since these operations cannot be interpreted on the whole .ini file under Preferences.

Reading and writing values

Values can read and write like any other Preferences node, there is no differences.

    void sample01(Ini ini) throws IOException
    {
        Preferences prefs = new IniPreferences(ini);
        Preferences bashful = prefs.node("bashful");
        String home = bashful.get("homeDir", "/home");
        int age = bashful.getInt("age", -1);

        bashful.putDouble("weight", 55.6);