Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

parsing

ini

I am writing a drop-in replacement for a legacy application in Java. One of the requirements is that the ini files that the older application used have to be read as-is into the new Java Application. The format of this ini files is the common windows style, with header sections and key=value pairs, using # as the character for commenting.

I tried using the Properties class from Java, but of course that won't work if there is name clashes between different headers.

So the question is, what would be the easiest way to read in this INI file and access the keys?

like image 718
Mario Ortegón Avatar asked Oct 10 '08 09:10

Mario Ortegón


People also ask

How do I read a .INI file?

How to Open and Edit INI Files. It's not a common practice for people to open or edit INI files, but they can be opened and changed with any text editor. Just double-clicking it will automatically open it in the Notepad application in Windows.

What is INI file in Java?

Learn how to work with INI (configuration files) 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.

What is INI parser?

Introduction. iniParser is a simple C library offering ini file parsing services. The library is pretty small (less than 1500 lines of C) and robust, and does not depend on any other external library to compile. It is written in ANSI C and should compile on most platforms without difficulty.

How do I structure an INI file?

The INI file consists of sections and keys. The name of a section in the *. ini file is always entered inside the square brackets. Each section contains several keys (the key must be always assigned to the section that begins in the file before this key).


1 Answers

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)); 
like image 150
Mario Ortegón Avatar answered Sep 30 '22 20:09

Mario Ortegón