Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2004
Views 118,999
Downloads 2,082
Bookmarked 21 times

An Enhanced INI File Class for Java

By | 26 Aug 2004 | Article
A INI file manipulation class with support for environment variables

Introduction

This article is about a Java class to read and write the Windows INI files. One good feature of this class is support for environment variables, i.e. this class allows one 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 DBLib variable is read, the class will try to retrieve the value for %ORACLE_HOME% variable. Let's say on your system %ORACLE_HOME% points to C:\Oracle directory, then the 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 main method of 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.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.

  1. setDateFormat - This method allows one to set the date format to be used while converting date strings to date data type and vice versa.
  2. setTimeFormat - This method allows one 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 the INI property values. All these methods requires that the INI section name and the property name be passed as input parameters.

  1. getBooleanProperty - Returns boolean value
  2. getStringProperty - Returns string value
  3. getIntegerProperty - Returns int value
  4. getLongProperty - Returns long value
  5. getDoubleProperty - Returns double value
  6. getDateProperty - Returns java.util.Date value
  7. getTimestampProperty - Returns java.sql.Timestamp value

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

  1. getAllSectionNames - Returns a string array of section names
  2. getAllPropertyNames - Returns a string array of section names. This method requires a section name as an input parameter.
  3. removeProperty - Removes specified property from the specified section
  4. removeSection - Removes the specified section
  5. 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 supports the 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 the 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 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

License

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

About the Author

Prasad Khandekar

Architect
Fundtech INDIA Ltd.
India India

Member

I am a software professional with over 16 years of commercial business applications design and development experience.
 
My programming experience includes Java, .NET, Classic VB, ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.
 
For last 5 years I am mostly working on Java platform.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionException in function save() if using Zero Length Comments PinmemberMember 84131280:20 21 Nov '11  
GeneralWhy it is case-sensitive?! PinmemberDmitry_Bond8:54 10 Aug '10  
GeneralRe: Why it is case-sensitive?! Pinmemberpaul_nicholls19:31 31 May '11  
GeneralRe: Why it is case-sensitive?! Pinmembercharan.kolhe8:37 13 Aug '11  
GeneralMy vote of 2 Pinmemberdvhh13:28 2 Mar '09  
Questionnew function save as can be added as below PinmemberSatish K Patel17:59 14 Nov '08  
GeneralMay I use this code in a commercial product. Pinmemberxsunemp3:42 13 Nov '07  
AnswerRe: May I use this code in a commercial product. PinmemberPrasad Khandekar1:58 17 Nov '07  
GeneralRe: May I use this code in a commercial product. PinmemberMember 48046901:19 9 Mar '11  
GeneralRe: May I use this code in a commercial product. Pinmemberjoewille23:53 10 Apr '11  
QuestionThe class does not compile :( Pinmemberhmoner14:22 13 Nov '05  
AnswerRe: The class does not compile :( PinmemberPrasad Khandekar2:29 13 Jan '06  
GeneralsetSectionComments Pinmemberanarinsky13:22 18 Aug '05  
GeneralRe: setSectionComments PinmemberPrasad Khandekar5:04 23 Aug '05  
GeneralRe: setSectionComments PinmemberVengoal18:26 6 Sep '05  
Generalanother implementation PinsussAnonymous20:26 17 Mar '05  
GeneralSeemingly duplicate ini entries Pinmembercgoudie5:05 3 Jan '05  
GeneralRe: Seemingly duplicate ini entries Pinmemberklizza13:14 25 Jan '05  
Generalnew bug PinmemberDavid Sourek11:14 27 Dec '04  
GeneralRe: new bug Pinmemberlzq12333523:23 3 Jun '09  
GeneralHandling of \n \r and \t PinmemberJens Scheidtmann4:56 27 Aug '04  
GeneralRe: Handling of \n \r and \t PinmemberPrasad Khandekar5:37 27 Aug '04  
GeneralBug fixes and additional improvements PinmemberJohan Natt och Dag2:23 13 Aug '04  
GeneralRe: Bug fixes and additional improvements PinmemberPrasad Khandekar2:10 20 Aug '04  
GeneralRe: Bug fixes and additional improvements PinmemberJohan Natt och Dag3:50 20 Aug '04  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 27 Aug 2004
Article Copyright 2004 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid