org.comedia.util
Class CIniFile

java.lang.Object
  |
  +--org.comedia.util.CIniFile

public class CIniFile
extends java.lang.Object

Emulates Windows INI file interface. It loads and entire ini file into memory and allows all operations to be performed on the memory image. The image can then be written out to the disk file.

Example of usage:

 CIniFile iniFile = new CIniFile("test.ini");

 boolean p1 = iniFile.readBool("Section 1", "P1", false);
 System.out.println("P1 = " + p1);
 iniFile.writeBool("Section 1", "P1", !p1);

 System.out.println("Section 2, Parameter 1 = " 
   + iniFile.readString("Other X", "Param1", ""));

 System.out.println("Section 2, Parameter 2 = " 
   + iniFile.readInteger("Section 2", "P2", 123));

 System.out.println("Section 2, Parameter 3 = " 
  + iniFile.readDateTime("Section 2", "P3", new Date()));

 iniFile.writeString("Section 2", "P1", "..\"xxx\"...");
 iniFile.writeDateTime("Section 2", "P3", new Date());

 iniFile.flush();
 


Constructor Summary
CIniFile()
          Constructs an empty class with default properties.
CIniFile(java.io.File file)
          Constructs this class with specified class name.
CIniFile(java.lang.String fileName)
          Constructs this class with specified class name.
CIniFile(java.net.URL url)
          Constructs this class with specified class name.
 
Method Summary
 void clear()
          Removes all sections from this ini file.
 void deleteKey(java.lang.String section, java.lang.String ident)
          Removes the specified key from the section.
 void eraseSection(java.lang.String section)
          Removes specified section from this ini file.
 void flush()
          Flushes this ini file to disk file.
 boolean isSectionExists(java.lang.String section)
          Tests if section exists in this ini file.
 void loadFromStream(java.io.InputStream stream)
          Loads the content from the specified input stream.
static void main(java.lang.String[] args)
          The main procedure for test purposes.
 boolean readBool(java.lang.String section, java.lang.String ident, boolean def)
          Reads boolean value from this ini file.
 java.util.Date readDateTime(java.lang.String section, java.lang.String ident, java.util.Date def)
          Reads dateTime value from this ini file.
 double readFloat(java.lang.String section, java.lang.String ident, double def)
          Reads float value from this ini file.
 int readInteger(java.lang.String section, java.lang.String ident, int def)
          Reads integer value from this ini file.
 java.lang.String readString(java.lang.String section, java.lang.String ident, java.lang.String def)
          Reads string value from this ini file.
 void rename(java.io.File file, boolean reload)
          Renames or reload this ini file.
 void rename(java.lang.String fileName, boolean reload)
          Renames or reload this ini file.
 void rename(java.net.URL url, boolean reload)
          Renames or reload this ini file.
 void saveToStream(java.io.OutputStream stream)
          Saves ini file into output stream.
 java.lang.String unwrapString(java.lang.String s)
          Converts a string from escape format limited with quotes into oridinary (local) presentation.
 boolean valueExists(java.lang.String section, java.lang.String ident)
          Tests if key exists in the specified section in this ini file.
 void writeBool(java.lang.String section, java.lang.String ident, boolean value)
          Writes boolean value with specified key into the section in this ini file.
 void writeDateTime(java.lang.String section, java.lang.String ident, java.util.Date value)
          Writes dateTime value with specified key into the section in this ini file.
 void writeFloat(java.lang.String section, java.lang.String ident, double value)
          Writes float value with specified key into the section in this ini file.
 void writeInteger(java.lang.String section, java.lang.String ident, int value)
          Writes integer value with specified key into the section in this ini file.
 void writeString(java.lang.String section, java.lang.String ident, java.lang.String value)
          Writes string value with specified key into the section in this ini file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CIniFile

public CIniFile()
Constructs an empty class with default properties.

CIniFile

public CIniFile(java.lang.String fileName)
         throws java.io.IOException
Constructs this class with specified class name.
Parameters:
fileName - a name of a ini file.

CIniFile

public CIniFile(java.io.File file)
         throws java.io.IOException
Constructs this class with specified class name.
Parameters:
file - a file object of a ini file.

CIniFile

public CIniFile(java.net.URL url)
         throws java.io.IOException
Constructs this class with specified class name.
Parameters:
url - an url which points to file.
Method Detail

clear

public void clear()
Removes all sections from this ini file.

deleteKey

public void deleteKey(java.lang.String section,
                      java.lang.String ident)
Removes the specified key from the section.
Parameters:
section - a name of the section.
ident - the name of removed key.

eraseSection

public void eraseSection(java.lang.String section)
Removes specified section from this ini file.
Parameters:
section - the name of removed section.

valueExists

public boolean valueExists(java.lang.String section,
                           java.lang.String ident)
Tests if key exists in the specified section in this ini file.
Parameters:
section - the name of the section.
ident - the name of the tested key.

isSectionExists

public boolean isSectionExists(java.lang.String section)
Tests if section exists in this ini file.
Parameters:
section - teh name of the tested section.

loadFromStream

public void loadFromStream(java.io.InputStream stream)
                    throws java.io.IOException
Loads the content from the specified input stream.
Parameters:
stream - the input stream to read the ini file.

unwrapString

public java.lang.String unwrapString(java.lang.String s)
Converts a string from escape format limited with quotes into oridinary (local) presentation.
Parameters:
s - a string in escape format.

readString

public java.lang.String readString(java.lang.String section,
                                   java.lang.String ident,
                                   java.lang.String def)
Reads string value from this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
def - a default value, if this key is not exists.

rename

public void rename(java.lang.String fileName,
                   boolean reload)
            throws java.io.IOException
Renames or reload this ini file.
Parameters:
fileName - the new file name.
reload - if TRUE the file will be reloaded with new contents, otherwise it will be stored in the new file with old contents.

rename

public void rename(java.io.File file,
                   boolean reload)
            throws java.io.IOException
Renames or reload this ini file.
Parameters:
file - object the new file name.
reload - if TRUE the file will be reloaded with new contents, otherwise it will be stored in the new file with old contents.

rename

public void rename(java.net.URL url,
                   boolean reload)
            throws java.io.IOException
Renames or reload this ini file.
Parameters:
url - a new url of the file name.
reload - if TRUE the file will be reloaded with new contents, otherwise it will be stored in the new file with old contents.

saveToStream

public void saveToStream(java.io.OutputStream stream)
                  throws java.io.IOException
Saves ini file into output stream.
Parameters:
ouput - stream to save this ini file.

flush

public void flush()
           throws java.io.IOException
Flushes this ini file to disk file.

writeString

public void writeString(java.lang.String section,
                        java.lang.String ident,
                        java.lang.String value)
Writes string value with specified key into the section in this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
value - a value of the key.

readInteger

public int readInteger(java.lang.String section,
                       java.lang.String ident,
                       int def)
Reads integer value from this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
def - a default value, if this key is not exists.

writeInteger

public void writeInteger(java.lang.String section,
                         java.lang.String ident,
                         int value)
Writes integer value with specified key into the section in this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
value - a value of the key.

readBool

public boolean readBool(java.lang.String section,
                        java.lang.String ident,
                        boolean def)
Reads boolean value from this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
def - a default value, if this key is not exists.

writeBool

public void writeBool(java.lang.String section,
                      java.lang.String ident,
                      boolean value)
Writes boolean value with specified key into the section in this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
value - a value of the key.

readFloat

public double readFloat(java.lang.String section,
                        java.lang.String ident,
                        double def)
Reads float value from this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
def - a default value, if this key is not exists.

writeFloat

public void writeFloat(java.lang.String section,
                       java.lang.String ident,
                       double value)
Writes float value with specified key into the section in this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
value - a value of the key.

readDateTime

public java.util.Date readDateTime(java.lang.String section,
                                   java.lang.String ident,
                                   java.util.Date def)
Reads dateTime value from this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
def - a default value, if this key is not exists.

writeDateTime

public void writeDateTime(java.lang.String section,
                          java.lang.String ident,
                          java.util.Date value)
Writes dateTime value with specified key into the section in this ini file.
Parameters:
section - the name of the section.
ident - the key name of the value.
value - a value of the key.

main

public static void main(java.lang.String[] args)
The main procedure for test purposes.
Parameters:
args - the command line arguments.