What Is the Easiest Way to Parse an Ini File in Java

What is the easiest way to parse an INI file in Java?

The library I've used is ini4j. It is lightweight and parses the ini files with ease. Also it uses no esoteric dependencies to 10,000 other jar files, as one of the design goals was to use only the standard Java API

This is an example on how the library is used:

Ini ini = new Ini(new File(filename));
java.util.prefs.Preferences prefs = new IniPreferences(ini);
System.out.println("grumpy/homePage: " + prefs.node("grumpy").get("homePage", null));

How to parse ini file with sections in Java?

I can suggest you ini4j. This is small and very easy to use library. Just download files or include dependency into your maven config:

<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
</dependency>

Now to load .ini file just do:

Wini ini = new Wini(new File("your file path"));

Examples of usage:

//output names of all sections    
Collection<Profile.Section> list = ini.values();
for(Profile.Section section : list){
System.out.println(section.getName());
}

//fetch and output option value
String value = ini.fetch("link#1", "alias");
System.out.println(value);

//output keys of all options of one section
Profile.Section section = ini.get("link#1");
for(String key : section.keySet()){
System.out.println(key);
}

What is the easiest way to parse an INI File in C?

What problems are you having with using iniparser? I just tried it. I first did make in the iniparser directory, and the code was built. To use the library, I did the following:


gcc test.c ./libiniparser.a

This was because I had created the test program in the same directory as the library. When you include iniparser.h in C++, make sure to do the following:

extern "C"
{
#include "src/iniparser.h"
}

Parsing error after '#' line in ini file

You can do the same with Properties:

 Properties p = new Properties();
p.load(new FileInputStream("user.props"));
System.out.println("user = " + p.getProperty("DBuser"));
System.out.println("password = " + p.getProperty("DBpassword"));
System.out.println("location = " + p.getProperty("DBlocation"));

where the .ini file is:

# this a comment
! this a comment too
DBuser=anonymous
DBpassword=&8djsx
DBlocation=bigone

How can I read INI file in Python without configparser?

Have you tried just opening it as a json file? Since the ini file is a text file, it should * just work *

import json

# I saved your example in a text file called test.ini and
# put it in the same folder as this script
with open('test.ini', 'r') as f:
data = json.load(f)

The contents of your ini file will be in data, which will be a dictionary.



Related Topics



Leave a reply



Submit