How to read (parse) from and write to INI files easily in Java

How to read (parse) from and write to INI files easily in Java

The configuration files in INI format allows the user to easily modify aspects of applications dinamically through a file. In this article, we'll show you how to use the Ini4j library to parse and write to INI file easily in Java.

1. Include Ini4j library

Java by default doesn't offer any integrated functionality to parse or create INI files, instead you will need to rely on a third party library/package. In this tutorial, we'll use the Ini4j library. The ini4j is a simple Java API for handling configuration files in Windows .ini format. Additionally, the library includes Java Preferences API implementation based on the .ini file. You can either download the jar file of the package from the maven repository and include it manually in your project or if your project is maven based, you may edit the pom.xml file and add the dependency:

<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j -->
<dependency>
    <groupId>org.ini4j</groupId>
    <artifactId>ini4j</artifactId>
    <version>0.5.4</version>
</dependency>

For more information about this package, please visit the official repository at Maven here or the official website. After including the library, you will be able to import the package with import org.ini4j.*; in your code.

2. Reading data from a ini file

Assuming we have an ini file (myinifile.ini) with the following content:

; Last modified 1 January 2019 by John Doe
; Example of INI File for Java
; myinifile.ini
[owner]
name=John Doe
organization=Acme Widgets Inc.
age=24
height=176

[database]
server=192.0.2.62     
port=143
file="payroll.dat"

It's pretty easy to obtain the values using the Ini4j interface. Basically, you need to instantiate a Wini class that expects as first argument a File class with the path of the ini file that you want to parse, the returned object will allow you to interact with the API of this library, making it pretty easy to obtain the values from it. The method that you need to use from the object is the get, this method expects up to 3 arguments:

  • The name of the section that you want to get the value from.
  • The name of the property inside the selected section.
  • The type of value that will be retrieved (applies only for primitive values, if none set, a String will be returned).

For example:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            int age = ini.get("owner", "age", int.class);
            double height = ini.get("owner", "height", double.class);
            String server = ini.get("database", "server");
            
            
            System.out.print("Age: " + age + "\n");
            System.out.print("Geight: " + height + "\n");
            System.out.print("Server IP: " + server + "\n");
        // To catch basically any error related to finding the file e.g
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

3. Writing data to a ini file

Writing to a file implies the insert/remove/update tasks:

Defining properties values

To change the value of a property within a section, use method put from the instance of the Wini class. This method expects up to 3 arguments:

  • the section from that you want to you want to update the property
  • The name of the property that you want to change
  • The new value of the property

Finally save changes to the file with the store method:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            
            ini.put("block_name", "property_name", "value");
            ini.put("block_name", "property_name_2", 45.6);
            ini.store();
        // To catch basically any error related to writing to the file
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

Removing an entire section

If you are willing to delete an entire section from the file, use the remove method that expects the name of the section that you want to delete, then save the changes with the store method:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            
            ini.remove("section_name");
            ini.store();
        // To catch basically any error related to writing to the file
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

Happy coding !

This could interest you
Become a more social person