This page contains an explanation of the advanced topics of the Java INI Package, along with code examples and explanations. Although this document aims to be as concise as possible, developers are recommended to consult the javadoc for a more complete and upto date documentation of Java INI Package.
8. Case-sensitivity
8.1. Default Case-sensitivity (Case-insensitive)
8.2. Specifying Case-sensitive
By default IniFile
s, IniSection
s and IniItem
s are treated
as case-insensitive, that is, the case of section names or item names are irrelevant. To
demonstrate this, take a look at the example below.
import
org.dtools.ini.*;public class
DefaultCaseExample
{public static void
main
( String
[] args
) {//***********************************************************************
// Step 1 - create an IniFile
//***********************************************************************
IniFile
ini
= new
BasicIniFile
();System
.out
.println
( "IniFile created
" );System
.out
.println
( ini
.getNumOfSections
() );System
.out
.println
();//***********************************************************************
// Step 2 - add a new section called "countries"
//***********************************************************************
IniSection
originalSection
= ini
.addSection
( "countries
" );System
.out
.println
( "Added setion: countries
" );System
.out
.println
( ini
.getNumOfSections
() );System
.out
.println
();//***********************************************************************
// Step 3 - add a new section with same word but different cases
//***********************************************************************
IniSection
newSection
= ini
.addSection
( "cOuNtRiEs
" );System
.out
.println
( "Added setion: cOuNtRiEs
" );System
.out
.println
( ini
.getNumOfSections
() );System
.out
.println
();String
output
= "Method addSection(\"cOuNtRiEs\") returned
";if
( newSection
== null
) {System
.out
.println
( output
+ "null
" );else
{System
.out
.println
( output
+ "section
" );The above code first creates an IniFile
object and then prints the number of
sections within the IniFile
with the following line:
System
.out
.println
( ini
.getNumOfSections
() );
The code then adds a section called "countries" and again prints out the number of sections
the IniFile
object has. Then, the code adds yet another section called
"cOuNtRiEs" and again prints out the number of sections the IniFile
object
has. Finally, the code tests to see if the IniSection
object returned is
null
. The output of this code is:
As you can see, even though two sections were added, the IniFile
report that it
contains only one section. This is because when "cOuNtRiEs
" is added, the
IniFile
checks to see if it already contains a section with that name (remember
that it ignores the case sensitivity), and if it does a section with that name, the section
isn't added and the method returns null
.
The purpose to all this is for when a user want to read an INI file from the card disk, and when he/she attempts to retrieve the sections and items, they do not need to concern themselves with getting the cases correct. However, this causes problems if an INI file is case-sensitive, expecially if it contains two different sections with the same items, as in the following example:
[user-dan]
username
= dan
name
= Daniel Thompson
email
= dan@company.com
[user-dan_47]
username
= dan_47
name
= Danny Dalton
email
= danny@company.com
[user-Dan]
username
= Dan
name
= Daniel Simmons
email
= dans@company.com
If an IniFileReader
object read the above INI file and treated it as
case-insensitive, then the values for the items in the section "user-Dan" would override
the values for the items in the section "user-dan", and would produce an IniFile
that represents something like this:
[user-dan]
username
= Dan
name
= Daniel Simmons
email
= dans@company.com
[user-dan_47]
username
= dan_47
name
= Danny Dalton
email
= danny@company.com
This problem can easily be resolved by simply specifing that an IniFile
is case
sensitive in the constructor with a boolean value.
IniFile
ini
= new
BasicIniFile
( true
);
If the boolean value is true
then the IniFile
is treated as
case-sensitive. However, if the value if false
or if no boolean value is given,
then the IniFile
is treated as case-insensitive. For example, have a look as
the following code, which is the same as the code from the previous section, only now the
IniFile
is case-sensitive.
IniFile
ini
= new
BasicIniFile
( true
);System
.out
.println
( "IniFile created
" );System
.out
.println
( ini
.getNumOfSections
() );IniSection
originalSection
= ini
.addSection
( "countries
" );System
.out
.println
( "Added setion: countries
" );System
.out
.println
( ini
.getNumOfSections
() );IniSection
newSection
= ini
.addSection
( "cOuNtRiEs
" );System
.out
.println
( "Added setion: cOuNtRiEs
" );System
.out
.println
( ini
.getNumOfSections
() );System
.out
.println
();if
( newSection
== null
) {System
.out
.println
( "Method addSection("cOuNtRiEs") returned null
" );else
{System
.out
.println
( "Method addSection("cOuNtRiEs") returned section
" );Which, compared to the output in section 8.1, produces the following output:
Notice that, unlike the code from section 8.1, this code now adds "cOuNtRiEs
"
successfully and treats "countries
" and "cOuNtRiEs
" as seperate
sections.
// Step 1 - create a case-sensitive IniFile
//***********************************************************************