Package org.kissweb

Class IniFile

java.lang.Object
org.kissweb.IniFile

public class IniFile extends Object
Class to deal with ini files. These are text-based property files broken into sections. Each section may have any number of key/value pairs. You can also dispense with the sections if you only have one.

The file can also have blank and comment lines. Comment lines start with a semicolon, colon, hash, dash, or astrix. Keys and values may be quoted with either single or double quotes.

This class is thread-safe.

Files look as follows:
     [section]
     key = value
     key2 = value2
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new ini file in memory.
    IniFile(String fname)
    Create a new ini file in memory that will be saved to the specified file.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(String key)
    Retrieves the string value of a key from the ini file from the null section.
    get(String section, String key)
    Returns the value of a key from a specified section in the ini file.
    boolean
    Retrieves the Boolean value of a key from the ini file from the null section.
    boolean
    getBoolean(String section, String key)
    Retrieves the boolean value of a key from a specified section in the ini file.
    Retrieves the Character value of a key from the ini file from the null section.
    getChar(String section, String key)
    Retrieves the first character of a key from a specified section in the ini file.
    int
    Retrieves the integer value representing a date as YYYYMMDD of a key from the ini file from the null section.
    int
    getDateInt(String section, String key)
    Retrieves the integer value of a date key from a specified section in the ini file.
    Retrieves the Double value of a key from the ini file from the null section.
    getDouble(String section, String key)
    Retrieves the double value of a key from a specified section in the ini file.
    Returns the filename of the ini file.
    Retrieves the Integer value of a key from the ini file from the null section.
    getInt(String section, String key)
    Retrieves the integer value of a key from a specified section in the ini file.
    int
    Retrieves the integer value representing a date as HHMM of a key from the ini file from the null section.
    int
    getTimeInt(String section, String key)
    Retrieves the integer value of a time key from a specified section in the ini file.
    static IniFile
    load(String fname)
    Load an ini file from a disk file.
    static void
    main(String[] args)
    This test program can be run from the command-line as follows:

    java -cp work/Kiss.jar org.kissweb.IniFile
    void
    put(String key, boolean value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String key, char value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String key, double value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String key, int value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String key, long value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String key, String value)
    Puts a key-value pair into the ini file in the null section.
    void
    put(String section, String key, boolean value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, char value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, double value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, int value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, long value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, String value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String section, String key, Date value)
    Puts a key-value pair into a specified section of the ini file.
    void
    put(String key, Date value)
    Puts a key-value pair into the ini file in the null section.
    void
    Removes the key-value pair associated with the given key from the ini file in the null section.
    void
    removeKey(String section, String key)
    Removes the specified key/value pair from the specified section of the ini file.
    void
    Removes the value associated with the given key from the ini file in the null section.
    void
    removeValue(String section, String key)
    Removes the specified key from the specified section of the ini file.
    void
    Save the in-memory ini file to file it was read from.
    void
    save(String fname)
    Save the in-memory ini file to the specified file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IniFile

      public IniFile()
      Create a new ini file in memory. If it is to be saved, the file name to be used can be specified later.
    • IniFile

      public IniFile(String fname)
      Create a new ini file in memory that will be saved to the specified file.
      Parameters:
      fname -
  • Method Details

    • load

      public static IniFile load(String fname) throws IOException
      Load an ini file from a disk file.
      Parameters:
      fname -
      Returns:
      Throws:
      IOException
    • getFilename

      public String getFilename()
      Returns the filename of the ini file.
    • get

      public String get(String section, String key)
      Returns the value of a key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the value of the key if found, null otherwise
    • getInt

      public Integer getInt(String section, String key)
      Retrieves the integer value of a key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the integer value of the key if found, null otherwise
    • getChar

      public Character getChar(String section, String key)
      Retrieves the first character of a key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the first character of the key if found, null otherwise
    • getBoolean

      public boolean getBoolean(String section, String key)
      Retrieves the boolean value of a key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      true if the value starts with 't', '1', or 'y', false otherwise
    • getDouble

      public Double getDouble(String section, String key)
      Retrieves the double value of a key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the double value of the key if found, null otherwise
    • getDateInt

      public int getDateInt(String section, String key)
      Retrieves the integer value of a date key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the integer value of the key if found (YYYYMMDD), 0 otherwise
    • getTimeInt

      public int getTimeInt(String section, String key)
      Retrieves the integer value of a time key from a specified section in the ini file.
      Parameters:
      section - the section to search for the key
      key - the key to search for
      Returns:
      the integer value of the key if found (HHMM), 0 otherwise
    • put

      public void put(String section, String key, String value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, int value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, double value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, boolean value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, char value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, long value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String section, String key, Date value)
      Puts a key-value pair into a specified section of the ini file.
      Parameters:
      section - the section to put the key-value pair into
      key - the key to put into the section
      value - the value to put into the section
    • save

      public void save(String fname) throws IOException
      Save the in-memory ini file to the specified file.
      Parameters:
      fname -
      Throws:
      IOException
    • save

      public void save() throws IOException
      Save the in-memory ini file to file it was read from.
      Throws:
      IOException
    • removeValue

      public void removeValue(String section, String key)
      Removes the specified key from the specified section of the ini file.
      Parameters:
      section - the section to remove the key from
      key - the key to remove
    • removeKey

      public void removeKey(String section, String key)
      Removes the specified key/value pair from the specified section of the ini file.
      Parameters:
      section - the section to remove the key from
      key - the key to remove
    • get

      public String get(String key)
      Retrieves the string value of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, null otherwise
    • getInt

      public Integer getInt(String key)
      Retrieves the Integer value of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, null otherwise
    • getChar

      public Character getChar(String key)
      Retrieves the Character value of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, null otherwise
    • getBoolean

      public boolean getBoolean(String key)
      Retrieves the Boolean value of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, false otherwise
    • getDouble

      public Double getDouble(String key)
      Retrieves the Double value of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, null otherwise
    • getDateInt

      public int getDateInt(String key)
      Retrieves the integer value representing a date as YYYYMMDD of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, 0 otherwise
    • getTimeInt

      public int getTimeInt(String key)
      Retrieves the integer value representing a date as HHMM of a key from the ini file from the null section.
      Parameters:
      key - the key to search for
      Returns:
      the value of the key if found, 0 otherwise
    • put

      public void put(String key, String value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, int value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, double value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, boolean value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, char value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, long value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • put

      public void put(String key, Date value)
      Puts a key-value pair into the ini file in the null section.
      Parameters:
      key - the key to put into the section
      value - the value to put into the section
    • removeValue

      public void removeValue(String key)
      Removes the value associated with the given key from the ini file in the null section.
      Parameters:
      key - the key to remove the value for
    • removeKey

      public void removeKey(String key)
      Removes the key-value pair associated with the given key from the ini file in the null section.
      Parameters:
      key - the key to remove the value for
    • main

      public static void main(String[] args) throws IOException
      This test program can be run from the command-line as follows:

      java -cp work/Kiss.jar org.kissweb.IniFile
      Parameters:
      args -
      Throws:
      IOException