Class IniFile

java.lang.Object
com.iizix.util.IniFile

public class IniFile extends Object
Loads and handles retrieval of data in a file such as SERVER.INI with [sections] and "item=data". The "data" may only be strings (i.e. no decimal or hexadecimal data is yet supported.
Author:
Christopher Mindus
  • Constructor Details

  • Method Details

    • createNewIniFile

      public static IniFile createNewIniFile(String fileName)
      Creates a new editable INI file.
    • createNewIniFile

      public static IniFile createNewIniFile(boolean isCaseSensitive, String fileName)
      Creates a new editable INI file.
    • setCreateUntitledSectionHeadings

      public void setCreateUntitledSectionHeadings(boolean on)
      Sets if untitled sections headings should be created or not.
    • doCreateUntitledSectionHeadings

      public boolean doCreateUntitledSectionHeadings()
      Checked if untitled sections headings should be created.
    • needsReload

      public boolean needsReload()
      Check if this file needs to be reloaded.
    • reload

      public boolean reload(boolean force) throws IOException
      Reload internal state from external sources.
      Parameters:
      force - Force the reload flag.
      Returns:
      true for success, false for nothing done (failure is reported by IOException).
      Throws:
      IOException
    • getFileName

      public String getFileName()
      Gets the file name of the ini file.
    • getSections

      public Enumeration<String> getSections()
      Gets all the section elements in the file.
    • getItems

      public Enumeration<String> getItems(String section)
      Gets all the item elements in a section.
      Returns:
      null If section not found.
    • getSectionItems

      public HashMap<String,String> getSectionItems(String section)
      Gets a copy of the hash table of a section.
    • getSize

      public int getSize(String section)
      Gets number of elements in a section.
      Returns:
      0 If section not found.
    • getData

      public String getData(String section, String item)
      Gets an item string in a section.
      Returns:
      null If no item is found or if the item did not contain any data (i.e. as "ItemDoesNotHave An EqualSign".
    • getData

      public String getData(String section, String item, String defaultData)
      Gets an item string in a section with default data if not found.
    • getData

      public int getData(String section, String item, int defaultData)
      Gets a integer value from an item.
    • getData

      public boolean getData(String section, String item, boolean defaultData)
      Gets a boolean value from an item.
    • setItem

      public void setItem(String section, String item, int data)
      Sets the topic and item to a specific integer. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItem

      public void setItem(String section, String item, boolean data)
      Sets the topic and item to a specific boolean (0 or 1). If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItem

      public void setItem(String section, String item, String data)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItem

      public void setItem(String section, String item, String data, String newSectionHeadingText)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      This method doesn't do anything if the item already has the specified data.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, String[] items)
      Adds a set of items that does not include the equal sign to a topic. If the topic doesn't exist, it will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, String[] items, String newSectionHeadingText)
      Adds a set of items that does not include the equal sign to a topic. If the topic doesn't exist, it will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, Hashtable<String,String> itemDataPairs)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, Hashtable<String,String> itemDataPairs, String newSectionHeadingText)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, HashMap<String,String> itemDataPairs)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, HashMap<String,String> itemDataPairs, String newSectionHeadingText)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, Set<Map.Entry<String,String>> entries)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • setItems

      public void setItems(String section, Set<Map.Entry<String,String>> entries, String newSectionHeadingText)
      Sets the topic and item to a specific string. If the topic or item doesn't exist, they will be created. Note that the topic may be in any character case (and any existing entry will preserve the original case). New topics/items are added at the end of the topic or the file respectively.

      Use the method saveFile to save all changes made to the file.

    • removeAllItems

      public void removeAllItems(String section)
      Removes all items defined in a section.
    • deleteSection

      public boolean deleteSection(String name)
      Removes a section from the INI file. This will comment the section heading and remove all the uncommented items in it.
      Returns:
      true for success; false for failure.
    • renameSection

      public boolean renameSection(String oldName, String newName)
      Renames a section in the INI file. The section should not contain the [brackets]. To remove a section from the INI file, i.e. to comment it, just set newName to null. This will comment the section heading and remove all the uncommented items in it.
      Returns:
      true for success; false for failure.
    • hasChanged

      public boolean hasChanged()
      Checks if the INI file has changed and might need a "save".
    • saveFile

      public void saveFile() throws IOException
      Saves all changes (if any) made to the file. Before the save is performed, a backup file as "filename[.extension].bak" is created.
      Throws:
      IOException - if an I/O error occurs.
    • save2

      public void save2(XOutputFile out) throws IOException
      Saves the INI file to an open PhantomOutputFile without closing it.
      Throws:
      IOException - if an I/O error occurs.
    • save

      public void save(OutputStream out) throws IOException
      Saves the INI file to an OutputStream without closing it.
      Throws:
      IOException - if an I/O error occurs.