Click here to Skip to main content
11,430,053 members (73,429 online)
Click here to Skip to main content

An Enhanced INI File Class for Java

, 4 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
An INI file manipulation class with support for environment variables.


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
UserId = DBuser
Sid = DBSid
DBHost =
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");
    objINI.setStringProperty("Database", "UserId", "System");
    objINI.setStringProperty("Database", "Password", "Manager");
    objINI.setStringProperty("Database", "HostName", "DBServer");
    objINI.setIntegerProperty("Database", "Port", 1521);
    /* Save changes back to strFile */;
    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.

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.


  • 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prasad Khandekar
Software Developer (Senior) Freelancer
India India
I am a software professional with over 20 years of commercial business applications design and development experience.

My programming experience includes Java, Spring, .NET, Classic VB & ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.

From last 11 years I am mostly working with Java Technology. I am currently available to take up new assignments.

Comments and Discussions doesn't work in sdk 4.4.2 ? Pin
Jordi Lagares Roset28-Apr-15 1:38
memberJordi Lagares Roset28-Apr-15 1:38 
AnswerRe: doesn't work in sdk 4.4.2 ? Pin
Jordi Lagares Roset30-Apr-15 7:53
memberJordi Lagares Roset30-Apr-15 7:53 
GeneralMy vote of 5 Pin
Abhijit Thakre7-Apr-13 1:41
memberAbhijit Thakre7-Apr-13 1:41 
SuggestionException in function save() if using Zero Length Comments Pin
Member 841312821-Nov-11 1:20
memberMember 841312821-Nov-11 1:20 
GeneralWhy it is case-sensitive?! Pin
Dmitry_Bond10-Aug-10 9:54
memberDmitry_Bond10-Aug-10 9:54 
GeneralRe: Why it is case-sensitive?! Pin
paul_nicholls31-May-11 20:31
memberpaul_nicholls31-May-11 20:31 
GeneralRe: Why it is case-sensitive?! Pin
charan.kolhe13-Aug-11 9:37
membercharan.kolhe13-Aug-11 9:37 
GeneralMy vote of 2 Pin
dvhh2-Mar-09 14:28
memberdvhh2-Mar-09 14:28 
Questionnew function save as can be added as below Pin
Satish K Patel14-Nov-08 18:59
memberSatish K Patel14-Nov-08 18:59 
GeneralMay I use this code in a commercial product. Pin
xsunemp13-Nov-07 4:42
memberxsunemp13-Nov-07 4:42 
AnswerRe: May I use this code in a commercial product. Pin
Prasad Khandekar17-Nov-07 2:58
memberPrasad Khandekar17-Nov-07 2:58 
GeneralRe: May I use this code in a commercial product. Pin
Member 48046909-Mar-11 2:19
memberMember 48046909-Mar-11 2:19 
GeneralRe: May I use this code in a commercial product. Pin
joewille11-Apr-11 0:53
memberjoewille11-Apr-11 0:53 
QuestionThe class does not compile :( Pin
hmoner13-Nov-05 15:22
memberhmoner13-Nov-05 15:22 
AnswerRe: The class does not compile :( Pin
Prasad Khandekar13-Jan-06 3:29
memberPrasad Khandekar13-Jan-06 3:29 
GeneralsetSectionComments Pin
anarinsky18-Aug-05 14:22
memberanarinsky18-Aug-05 14:22 
GeneralRe: setSectionComments Pin
Prasad Khandekar23-Aug-05 6:04
memberPrasad Khandekar23-Aug-05 6:04 
GeneralRe: setSectionComments Pin
Vengoal6-Sep-05 19:26
memberVengoal6-Sep-05 19:26 
Generalanother implementation Pin
Anonymous17-Mar-05 21:26
sussAnonymous17-Mar-05 21:26 
GeneralSeemingly duplicate ini entries Pin
cgoudie3-Jan-05 6:05
membercgoudie3-Jan-05 6:05 
GeneralRe: Seemingly duplicate ini entries Pin
klizza25-Jan-05 14:14
memberklizza25-Jan-05 14:14 
Generalnew bug Pin
David Sourek27-Dec-04 12:14
memberDavid Sourek27-Dec-04 12:14 
GeneralRe: new bug Pin
lzq1233354-Jun-09 0:23
memberlzq1233354-Jun-09 0:23 
GeneralHandling of \n \r and \t Pin
Jens Scheidtmann27-Aug-04 5:56
memberJens Scheidtmann27-Aug-04 5:56 
GeneralRe: Handling of \n \r and \t Pin
Prasad Khandekar27-Aug-04 6:37
memberPrasad Khandekar27-Aug-04 6:37 
GeneralBug fixes and additional improvements Pin
Johan Natt och Dag13-Aug-04 3:23
memberJohan Natt och Dag13-Aug-04 3:23 
GeneralRe: Bug fixes and additional improvements Pin
Prasad Khandekar20-Aug-04 3:10
memberPrasad Khandekar20-Aug-04 3:10 
GeneralRe: Bug fixes and additional improvements Pin
Johan Natt och Dag20-Aug-04 4:50
memberJohan Natt och Dag20-Aug-04 4:50 
GeneralRe: Bug fixes and additional improvements Pin
Prasad Khandekar27-Aug-04 6:33
memberPrasad Khandekar27-Aug-04 6:33 
GeneralRe: Bug fixes and additional improvements Pin
klizza27-Aug-04 11:44
memberklizza27-Aug-04 11:44 
GeneralAnother Bug: NPE Pin
klizza23-Aug-04 9:52
memberklizza23-Aug-04 9:52 
GeneralRe: Bug fixes and additional improvements Pin
HLO8-Jun-05 7:04
memberHLO8-Jun-05 7:04 
GeneralRe: Bug fixes and additional improvements Pin
Anonymous8-Jun-05 12:11
sussAnonymous8-Jun-05 12:11 
Generalfew bugs in this class Pin
chcoust12-Jul-04 0:19
susschcoust12-Jul-04 0:19 
GeneralRe: few bugs in this class Pin
FayeStar24-Jul-04 2:11
memberFayeStar24-Jul-04 2:11 
GeneralRe: few bugs in this class Pin
Mappo3-Aug-04 5:00
memberMappo3-Aug-04 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 4 Apr 2013
Article Copyright 2004 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid