Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading INI File in JAVA

Tags:

java

I was trying to get values from an INI File and I'm writing the code in Java. So far, I have read about this function and yes, it is returning values from the INI File.

String property = properties.getProperty("property.name");

But this is the code that I need.

Is there a function in Java that is equivalent to the GetPrivateProfileString of C?

like image 620
Innistrad Avatar asked Jan 12 '23 01:01

Innistrad


1 Answers

No, there is no built-in initialization .INI file support in Java.

Java uses Properties files, which lays its data out like so:

propertyOne = 1
propertyTwo = 2

C uses Initialization files (.INI), which lays its data out in sections:

[Section]
propertyOne = 1
propertyTwo = 2

In Java, Properties don't have "sections", but can be divided up into sections by the same convention for naming packages:

section.propertyOne = 1
section.propertyTwo = 2

So you would call properties.getProperty("section.propertyOne");, where in INI you call the section first and then the property instead of as one.

INI files offer some more usefulness, but are not natively supported. A good 3rd party Java API for dealing with INI files is ini4j.

http://ini4j.sourceforge.net/

The reason for this is that .INI is a Windows-specific file. You won't really find .INI files on other operating systems, and Java is a cross-platform language.

like image 180
hotforfeature Avatar answered Jan 21 '23 08:01

hotforfeature