Reg Tutorial - Windows .REG file handling

Windows regedit commands .REG file format is very close to .ini format. [ini4j] provides org.ini4j.Reg class to model .REG format. This tutorial show the differences between Ini and Reg classes.

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

Loading and storing

There is nothing special with loading and storing data, it works exactly same as in Ini class. But while loading data, Reg class will strip .REG special values (double qoute around strings, type data from option, etc). So after loading a .REG file, you can use it exactly same way as Ini class. Ofcource if you store Reg class, it will put all above meta information int file, so the result will be a valid .REG file. You don't need to worry about file encoding, version in first line, etc,etc.

Assume you have a .REG file, with the following section/key:


As you see, "weight" and "age" is not simlpe strings. The "height" is a REG_DWORD type while "weight" is REG_EXPAND_SZ. Don't worry, Reg class take care about type conversion, you will access these as with regular .ini files:

    void sample01(File file) throws IOException
        Reg reg = new Reg(file);
        Reg.Key hive = reg.get(Reg.Hive.HKEY_CURRENT_USER.toString());
        Reg.Key bashful;

        bashful = hive.lookup("Software", "ini4j-test", "dwarfs", "bashful");

        // or ...
        bashful = hive.lookup("Software\\ini4j-test\\dwarfs\\bashful");

        // or even...
        bashful = reg.get("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\bashful");

        // read some data
        double weight = bashful.get("weight", double.class);  // = 45.7
        double height = bashful.get("height", double.class);  // = 98.8
        int age = bashful.get("age", int.class);  // = 67
        URI homePage = bashful.get("homePage", URI.class);  // = new URI("http://snowwhite.tale/~bashful");
        String homeDir = bashful.get("homeDir");  // = "/home/bashful"


When you load data into Reg class, it will preserve meta informations, such as type informations. If you create new values, by default these will have tpye REG_SZ. Ofcource you may specify type information for values.

    void sample02()
        Reg reg = new Reg();
        Reg.Key key = reg.add("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\sleepy");

        key.put("fortuneNumber", 99);
        key.putType("fortuneNumber", Reg.Type.REG_MULTI_SZ);

If you store reg object above, it will contains a section similar to this: