Class IniFileEditor

java.lang.Object
com.pnfsoftware.jeb.util.base.IniFileEditor

public class IniFileEditor extends Object
A reader-and-writer for ini configuration files.

Example of a valid ini file:

 [section1]

 foo

 bar

 [section2]
 x
 y
 [section3]
 [section4]
 z

 
  • Constructor Details

    • IniFileEditor

      public IniFileEditor(File file, Character commentChar) throws IOException
      Create an editor by reading an ini file.
      Parameters:
      file - ini file
      commentChar - optional full-line comment character
      Throws:
      IOException - if the file cannot be read
    • IniFileEditor

      public IniFileEditor(File file) throws IOException
      Create an editor by reading an ini file.
      Parameters:
      file - ini file
      Throws:
      IOException - if the file cannot be read
    • IniFileEditor

      public IniFileEditor(String str, Character commentChar)
      Create an editor from ini text.
      Parameters:
      str - ini text
      commentChar - optional full-line comment character
    • IniFileEditor

      public IniFileEditor(String str)
      Create an editor from ini text.
      Parameters:
      str - ini text
  • Method Details

    • setCommentChar

      public void setCommentChar(Character cc)
      Set a comment character for full-line comments.
      Parameters:
      cc - optional full-line comment character
    • getCommentChar

      public Character getCommentChar()
      Retrieve the optional comment character (full-line comments only).
      Returns:
      comment character, or null if none is set
    • update

      public void update() throws IOException
      Update the original ini file.
      Throws:
      IOException - if the file cannot be written
    • write

      public void write(File newFile) throws IOException
      Write the updated contents to a new ini file.
      Parameters:
      newFile - destination file
      Throws:
      IOException - if the file cannot be written
    • getSectionNames

      public List<String> getSectionNames()
      Retrieve the list of sections in the ini file.
      Returns:
      section names in file order
    • getSectionContents

      public List<String> getSectionContents(String sectionName)
      Retrieve the contents of a section.
      Parameters:
      sectionName - section name, or null for the pre-section header
      Returns:
      the section contents, never null; blank entries are not present
    • getSectionKeyValues

      public Map<String,String> getSectionKeyValues(String sectionName)
      Retrieve the key-value entries for the provided section.
      Parameters:
      sectionName - case-sensitive section name
      Returns:
      a collection, possibly empty
    • setSectionContents

      public void setSectionContents(String sectionName, List<String> contents)
      Set the contents of a new section. The section is created if it does not exist.
      Parameters:
      sectionName - section name (without bracket)
      contents - blank lines are not added
    • getValue

      public String getValue(String keyName)
      Retrieve a value.
      Parameters:
      keyName - key name
      Returns:
      the value, null if not found
    • getValue

      public String getValue(String sectionName, String keyName)
      Retrieve a value.
      Parameters:
      sectionName - section name
      keyName - key name
      Returns:
      the value, null if not found
    • getValue

      public String getValue(String sectionName, String keyName, String delimiter)
      Retrieve a value.
      Parameters:
      sectionName - section name, or null for the pre-section header
      keyName - key name
      delimiter - key-value delimiter
      Returns:
      the value, null if not found
    • setValue

      public boolean setValue(String sectionName, String keyName, String value)
      Set a value.
      Parameters:
      sectionName - section name, or null for the pre-section header
      keyName - key name
      value - value to set
      Returns:
      true if the value was new or a true update; false if the key-value pair already exists
    • setValue

      public boolean setValue(String sectionName, String keyName, String value, String delimiter)
      Set a value.
      Parameters:
      sectionName - section name, or null for the pre-section header
      keyName - key name
      value - value to set
      delimiter - key-value delimiter
      Returns:
      true if the value was new or a true update; false if the key-value pair already exists