Class IniFile


  • public class IniFile
    extends java.lang.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 Summary

      Constructors 
      ConstructorDescription
      IniFile​(boolean isCaseSensitive, XFile in)
      Loads the contents a the opened file (typically from a JAR or ZIP file).
      IniFile​(boolean isCaseSensitive, XFile in, boolean isEditable)
      Loads the contents a the opened file (typically from a JAR or ZIP file).
      IniFile​(boolean isCaseSensitive, java.lang.String fileName)
      The constructor loads the contents of the file.
      IniFile​(boolean isCaseSensitive, java.lang.String fileName, boolean isEditable)
      The constructor loads the contents of the file.
      IniFile​(XFile in)
      Loads the contents a the opened file (typically from a JAR or ZIP file).
      IniFile​(XFile in, boolean isEditable)
      Loads the contents a the opened file (typically from a JAR or ZIP file).
      IniFile​(java.lang.String fileName)
      The constructor loads the contents of the file.
      IniFile​(java.lang.String fileName, boolean isEditable)
      The constructor loads the contents of the file.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      static IniFilecreateNewIniFile​(boolean isCaseSensitive, java.lang.String fileName)
      Creates a new editable INI file.
      static IniFilecreateNewIniFile​(java.lang.String fileName)
      Creates a new editable INI file.
      booleandeleteSection​(java.lang.String name)
      Removes a section from the INI file.
      booleandoCreateUntitledSectionHeadings()
      Checked if untitled sections headings should be created.
      java.lang.StringgetData​(java.lang.String section, java.lang.String item)
      Gets an item string in a section.
      booleangetData​(java.lang.String section, java.lang.String item, boolean defaultData)
      Gets a boolean value from an item.
      intgetData​(java.lang.String section, java.lang.String item, int defaultData)
      Gets a integer value from an item.
      java.lang.StringgetData​(java.lang.String section, java.lang.String item, java.lang.String defaultData)
      Gets an item string in a section with default data if not found.
      java.lang.StringgetFileName()
      Gets the file name of the ini file.
      java.util.Enumeration<java.lang.String>getItems​(java.lang.String section)
      Gets all the item elements in a section.
      java.util.HashMap<java.lang.String,​java.lang.String>getSectionItems​(java.lang.String section)
      Gets a copy of the hash table of a section.
      java.util.Enumeration<java.lang.String>getSections()
      Gets all the section elements in the file.
      intgetSize​(java.lang.String section)
      Gets number of elements in a section.
      booleanhasChanged()
      Checks if the INI file has changed and might need a "save".
      booleanneedsReload()
      Check if this file needs to be reloaded.
      booleanreload​(boolean force)
      Reload internal state from external sources.
      voidremoveAllItems​(java.lang.String section)
      Removes all items defined in a section.
      booleanrenameSection​(java.lang.String oldName, java.lang.String newName)
      Renames a section in the INI file.
      voidsave​(java.io.OutputStream out)
      Saves the INI file to an OutputStream without closing it.
      voidsave2​(XOutputFile out)
      Saves the INI file to an open PhantomOutputFile without closing it.
      voidsaveFile()
      Saves all changes (if any) made to the file.
      voidsetCreateUntitledSectionHeadings​(boolean on)
      Sets if untitled sections headings should be created or not.
      voidsetItem​(java.lang.String section, java.lang.String item, boolean data)
      Sets the topic and item to a specific boolean (0 or 1).
      voidsetItem​(java.lang.String section, java.lang.String item, int data)
      Sets the topic and item to a specific integer.
      voidsetItem​(java.lang.String section, java.lang.String item, java.lang.String data)
      Sets the topic and item to a specific string.
      voidsetItem​(java.lang.String section, java.lang.String item, java.lang.String data, java.lang.String newSectionHeadingText)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.lang.String[] items)
      Adds a set of items that does not include the equal sign to a topic.
      voidsetItems​(java.lang.String section, java.lang.String[] items, java.lang.String newSectionHeadingText)
      Adds a set of items that does not include the equal sign to a topic.
      voidsetItems​(java.lang.String section, java.util.HashMap<java.lang.String,​java.lang.String> itemDataPairs)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.util.HashMap<java.lang.String,​java.lang.String> itemDataPairs, java.lang.String newSectionHeadingText)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.util.Hashtable<java.lang.String,​java.lang.String> itemDataPairs)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.util.Hashtable<java.lang.String,​java.lang.String> itemDataPairs, java.lang.String newSectionHeadingText)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.String>> entries)
      Sets the topic and item to a specific string.
      voidsetItems​(java.lang.String section, java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.String>> entries, java.lang.String newSectionHeadingText)
      Sets the topic and item to a specific string.
      • Methods inherited from class java.lang.Object

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

      • IniFile

        public IniFile​(java.lang.String fileName)
                throws java.io.IOException,
                       java.io.FileNotFoundException
        The constructor loads the contents of the file.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.FileNotFoundException - if the file is not found.
      • IniFile

        public IniFile​(boolean isCaseSensitive,
                       java.lang.String fileName)
                throws java.io.IOException,
                       java.io.FileNotFoundException
        The constructor loads the contents of the file.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.FileNotFoundException - if the file is not found.
      • IniFile

        public IniFile​(java.lang.String fileName,
                       boolean isEditable)
                throws java.io.IOException,
                       java.io.FileNotFoundException
        The constructor loads the contents of the file. Set isEditable to true if the contents of the file may be changed and later saved.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.FileNotFoundException - if the file is not found.
      • IniFile

        public IniFile​(boolean isCaseSensitive,
                       java.lang.String fileName,
                       boolean isEditable)
                throws java.io.IOException,
                       java.io.FileNotFoundException
        The constructor loads the contents of the file. Set isEditable to true if the contents of the file may be changed and later saved.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.FileNotFoundException - if the file is not found.
      • IniFile

        public IniFile​(XFile in)
                throws java.io.IOException
        Loads the contents a the opened file (typically from a JAR or ZIP file).
        Throws:
        java.io.IOException - if an I/O error occurs.
      • IniFile

        public IniFile​(boolean isCaseSensitive,
                       XFile in)
                throws java.io.IOException
        Loads the contents a the opened file (typically from a JAR or ZIP file).
        Throws:
        java.io.IOException - if an I/O error occurs.
      • IniFile

        public IniFile​(XFile in,
                       boolean isEditable)
                throws java.io.IOException
        Loads the contents a the opened file (typically from a JAR or ZIP file).
        Throws:
        java.io.IOException - if an I/O error occurs.
      • IniFile

        public IniFile​(boolean isCaseSensitive,
                       XFile in,
                       boolean isEditable)
                throws java.io.IOException
        Loads the contents a the opened file (typically from a JAR or ZIP file).
        Throws:
        java.io.IOException - if an I/O error occurs.
    • Method Detail

      • createNewIniFile

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

        public static IniFile createNewIniFile​(boolean isCaseSensitive,
                                               java.lang.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 java.io.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:
        java.io.IOException
      • getFileName

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

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

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

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

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

        public java.lang.String getData​(java.lang.String section,
                                        java.lang.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 java.lang.String getData​(java.lang.String section,
                                        java.lang.String item,
                                        java.lang.String defaultData)
        Gets an item string in a section with default data if not found.
      • getData

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

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

        public void setItem​(java.lang.String section,
                            java.lang.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​(java.lang.String section,
                            java.lang.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​(java.lang.String section,
                            java.lang.String item,
                            java.lang.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​(java.lang.String section,
                            java.lang.String item,
                            java.lang.String data,
                            java.lang.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​(java.lang.String section,
                             java.lang.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​(java.lang.String section,
                             java.lang.String[] items,
                             java.lang.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​(java.lang.String section,
                             java.util.Hashtable<java.lang.String,​java.lang.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​(java.lang.String section,
                             java.util.Hashtable<java.lang.String,​java.lang.String> itemDataPairs,
                             java.lang.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​(java.lang.String section,
                             java.util.HashMap<java.lang.String,​java.lang.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​(java.lang.String section,
                             java.util.HashMap<java.lang.String,​java.lang.String> itemDataPairs,
                             java.lang.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​(java.lang.String section,
                             java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.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​(java.lang.String section,
                             java.util.Set<java.util.Map.Entry<java.lang.String,​java.lang.String>> entries,
                             java.lang.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​(java.lang.String section)
        Removes all items defined in a section.
      • deleteSection

        public boolean deleteSection​(java.lang.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​(java.lang.String oldName,
                                     java.lang.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 java.io.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:
        java.io.IOException - if an I/O error occurs.
      • save2

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

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