org.equanda.util
Class IniFile

java.lang.Object
  extended by org.equanda.util.IniFile

public class IniFile
extends Object

Class to manipulate .ini files. Keys can be case dependent or case independent. Comments are discarded. File is kept in memory and needs to be explicitly saved.

Author:
Joachim Van der Auwera

Constructor Summary
IniFile()
          Creates a new reference to an .ini file.
IniFile(boolean useCase)
           
IniFile(File file)
          Creates a new reference to an INI file.
IniFile(InputStream s)
          Creates a new reference to an INI file.
IniFile(Reader r)
          Creates a new reference to an INI file.
IniFile(String filename)
          Creates a new reference to an .ini file.
IniFile(String filename, boolean useCase)
          Creates a new reference to an .ini file.
 
Method Summary
 void addSection(String section)
          Creates a new section in the .ini file.
 void addValue(String section, String key, String value)
          Add a key/value pair
 Hashtable<String,Hashtable<String,String>> getAll()
          Allows the reading of the entire .ini file.
 Hashtable<String,String> getSection(String section)
          Allows the reading of an entire section of configuration data.
 Enumeration<String> getSectionKeys(String section)
          Get all the keys in a particular section
 Enumeration<String> getSectionNames()
          Get an anumeration with all the section names
 String getValue(String section, String key)
          Read a specific key/value pair
 boolean isEmpty()
          Check whether the .ini file has something in it.
 void removeAll()
          Clear .ini file
 void removeSection(String section)
          Remove a section including all keys.
 void removeValue(String section, String key)
          Removes a key/value pair from given section
 void save()
          Save (overwrite) .ini file
 void save(String file)
          Writes out the current data to a file.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IniFile

public IniFile()
Creates a new reference to an .ini file. Empty file is created.


IniFile

public IniFile(boolean useCase)

IniFile

public IniFile(String filename)
        throws IOException
Creates a new reference to an .ini file. Reads in all the data.

Parameters:
filename - The filename of the .ini file.
Throws:
IOException - if anything goes wrong during read.

IniFile

public IniFile(String filename,
               boolean useCase)
        throws IOException
Creates a new reference to an .ini file. Reads in all the data.

Parameters:
filename - The filename of the .ini file.
useCase - should tags be case dependent?
Throws:
IOException - if anything goes wrong during read.

IniFile

public IniFile(File file)
        throws IOException
Creates a new reference to an INI file. Reads in all the INI data.

Parameters:
file - A File object for the INI file.
Throws:
IOException - if anything goes wrong during read.

IniFile

public IniFile(Reader r)
        throws IOException
Creates a new reference to an INI file. Reads in all the INI data.

Parameters:
r - a reader which contains the INI file.
Throws:
IOException - if anything goes wrong during read.

IniFile

public IniFile(InputStream s)
        throws IOException
Creates a new reference to an INI file. Reads in all the INI data.

Parameters:
s - an InputStream which contains the INI file.
Throws:
IOException - if anything goes wrong during read.
Method Detail

isEmpty

public boolean isEmpty()
Check whether the .ini file has something in it.

Returns:
whether section is empty or not

getValue

public String getValue(String section,
                       String key)
Read a specific key/value pair

Parameters:
section - section name
key - The key for which the value needs to be retrieved
Returns:
value or null if not exists

getSectionKeys

public Enumeration<String> getSectionKeys(String section)
Get all the keys in a particular section

Parameters:
section - section name
Returns:
enumeration with all the keys from the section or null

getSection

public Hashtable<String,String> getSection(String section)
Allows the reading of an entire section of configuration data.

Parameters:
section - section name
Returns:
Hashtable with all the key/value pairs for the section or null

getAll

public Hashtable<String,Hashtable<String,String>> getAll()
Allows the reading of the entire .ini file.

Returns:
The Hashtable containing the Hashtables for each section. Will be empty if no sections exist.

addValue

public void addValue(String section,
                     String key,
                     String value)
Add a key/value pair

Parameters:
section - section name, will be created if new
key - key
value - value

addSection

public void addSection(String section)
Creates a new section in the .ini file. If the section already exists or is not defined, then nothing happens.

Parameters:
section - section name

removeValue

public void removeValue(String section,
                        String key)
Removes a key/value pair from given section

Parameters:
section - section that the key and value is in.
key - The key to be removed.

removeSection

public void removeSection(String section)
Remove a section including all keys.

Parameters:
section - section to remove

removeAll

public void removeAll()
Clear .ini file


save

public void save()
          throws IOException
Save (overwrite) .ini file

Throws:
IOException - if an error occurs while writing.

save

public void save(String file)
          throws IOException
Writes out the current data to a file.

Parameters:
file - The file to save to.
Throws:
IOException - if an error occurs while writing.

getSectionNames

public Enumeration<String> getSectionNames()
Get an anumeration with all the section names

Returns:
enumeration of section names

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2007-2009. All Rights Reserved.