Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

An Enhanced INI File Class for Java

0.00/5 (No votes)
21 Feb 2017 4  
An INI file manipulation class with support for environment variables

Introduction

This article is about a Java class to read and write Windows INI files. One good feature of this class is its support for environment variables, i.e., this class allows us to include a reference to an environment variable while defining values in INI files. The following code snippet depicts this feature:

;Sample INI file
[Database]
UserId = DBuser
Sid = DBSid
DBHost = 145.101.56.32
DBPort = 1521
DBLib = %ORACLE_HOME%\lib\classes12.jar

When the DBLib variable is read, the class will try to retrieve the value for the %ORACLE_HOME% variable. Let's say on your system %ORACLE_HOME% points to the C:\Oracle directory, then DBLib will get expanded to C:\Oracle\lib\classes12.jar.

Using the Code

The following code snippet shows the usage of this class. The same code can also be found in the main method of the INIFile class.

public static void main(String[] pstrArgs)
{
    INIFile objINI = null;
    String  strFile = null;

    if (pstrArgs.length == 0) return;

    strFile = pstrArgs[0];
    /* Following call loads the strFile if it exists. */
    objINI = new INIFile(strFile);

    objINI.setStringProperty("Database", "SID", "ORCL", "Database SID");
    objINI.setStringProperty("Database", "UserId", "System", "User Id");
    objINI.setStringProperty("Database", "Password", "Manager", "Password");
    objINI.setStringProperty("Database", "HostName", "DBServer", "Server Host");
    objINI.setIntegerProperty("Database", "Port", 1521, "Server Port");
    /* Save changes back to strFile */
    objINI.save();
    objINI = null;
}

Date and Timestamps Usage

Since all data in INI files is stored as strings, the class provides the following methods to interpret the date and timestamp values in the correct manner.

  • setDateFormat - This method allows to set the date format to be used while converting date strings to date data type and vice versa.
  • setTimeFormat - This method allows to set the timestamp format to be used while converting timestamp strings to timestamp data type and vice versa.

For the supported date time formats, please refer to java.text.SimpleDateFormat.

Methods Summary

The class exposes the following public methods to access INI property values. All these methods require that the INI section name and property name be passed as input parameters.

  • getBooleanProperty - Returns boolean value
  • getStringProperty - Returns string value
  • getIntegerProperty - Returns int value
  • getLongProperty - Returns long value
  • getDoubleProperty - Returns double value
  • getDateProperty - Returns java.util.Date value
  • getTimestampProperty - Returns java.sql.Timestamp value

Additionally, the class also provides the following additional methods to retrieve the names of all the sections present in an INI file, names of all the properties present under a particular section, remove the property, remove a section, and save changes back to the disk. The load method is automatically called from the constructor.

  • getAllSectionNames - Returns a string array of section names
  • getAllPropertyNames - Returns a string array of section names. This method requires a section name as an input parameter.
  • removeProperty - Removes specified property from the specified section
  • removeSection - Removes the specified section
  • save - Persist changes back to INI file

I hope this will be useful to anyone who needs this functionality in Java.

Known Limitations

The class does not support property values which span across multiple lines. Duplicate properties in a section are not supported. The last property value will overwrite previous value.

Points of Interest

JDK 1.3 and 1.4 do not have built-in classes and/or methods to access environment variable values. Thanks to Mr. RĂ©al Gagnon for generously making the code available to read environment variables.

History

  • 01/07/2004 - Initial release
  • 07/07/2004 - Added support for environment variables and data type specific getters and setters
  • 08/07/2004 - Code correction, mostly typing mistakes and a condition in the getStringProperty method
  • 26/08/2004 - Lots of useful modifications:
    • Added support for section and property level comments
    • Introduced a separate class to represent a property
    • Added method to create a named section
    • Changed HashMap to LinkedHashMap, Sections/Properties now retain their order
    • Some of the method implementations/signatures are changed
  • 15/06/2015 - Now Requires JDK 6 & above
    • Section & Property names are now case-insensitive
    • Duplicate property names are handlled correctly, the last value overwrites the previous
    • Environment variables are now fetched using System.getEnv()
  • 23/06/2015 - Fixed an issue in comments not getting saved properly
  • 21/02/2017 - Fixed an issue and new capability.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here