Class FileBasedProperties

java.lang.Object
ca.corbett.extras.properties.Properties
ca.corbett.extras.properties.FileBasedProperties

public final class FileBasedProperties extends Properties
Extends the Properties class to provide for saving and loading of properties to and from a flat file on disk.
Since:
2022-05-10
Author:
scorbo2
  • Constructor Details

    • FileBasedProperties

      public FileBasedProperties(File file)
      Creates a new, empty FileBasedProperties pointed at the given File object. No actual disk i/o will be undertaken until save() or load() are invoked.
      Parameters:
      file - The File on disk which will store these properties. Will be created if needed.
  • Method Details

    • setCommentHeader

      public void setCommentHeader(String header)
      You can set an optional comment header that will be written out at the top of the file whenever save() is invoked. For a multi-line comment block, just embed "\n" as needed into the string. A null value disables the header (this is the default).
      Parameters:
      header - Any string value to be written out as a comment header.
    • hasCommentHeader

      public boolean hasCommentHeader()
      Indicates whether this FileBasedProperties instance has a comment header set.
      Returns:
      True if a comment header block has been specified in this instance.
    • getCommentHeader

      public String getCommentHeader()
      Returns the comment header block that was set in this FileBasedProperties.
      Returns:
      a comment header block, or null if one was not set.
    • setEagerSave

      public void setEagerSave(boolean eager)
      Sets whether this object should save eagerly (default is false). If true, then each time any of the various set methods are invoked, a disk save will be undertaken immediately. If false, the properties in memory are never saved to disk unless save() is invoked.
      Parameters:
      eager - Whether to save every time a property value is set, or to wait until save().
    • isEagerSave

      public boolean isEagerSave()
      Returns whether this instance is set to eagerly save changes or not.
      Returns:
      true if this instance is set to eagerly save.
    • load

      public void load() throws IOException
      Attempts to load property values from our file.
      Throws:
      IOException - If something goes wrong.
    • save

      public void save() throws IOException
      Saves the current properties to our file.
      Throws:
      IOException - If something goes wrong.
    • getFile

      public File getFile()
      Returns the File around which this instance is based.
      Returns:
      A File.
    • save

      public void save(String comments) throws IOException
      Saves the current properties to our file. You can set an optional comment block here which will be appended to the comment header set in this class (if any).
      Parameters:
      comments - An optional comment block (use \n in the string to add multiple lines).
      Throws:
      IOException - If something goes wrong.
    • remove

      public void remove(String name)
      Removes the property with the given name, if any.
      Overrides:
      remove in class Properties
      Parameters:
      name - The property name.
    • saveWithoutException

      public void saveWithoutException()
      Attempts to save without throwing an IOException -if one occurs, it will be logged without being rethrown.
    • saveWithoutException

      public void saveWithoutException(String comments)
      Attempts to save without throwing an IOException - if one occurs, it will be logged without being rethrown.
      Parameters:
      comments - An optional comment block (use \n in the string to add multiple lines).
    • setColor

      public void setColor(String name, Color value)
      Sets a Color property. Replaces any previous value for the given property name.
      Overrides:
      setColor in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setDouble

      public void setDouble(String name, Double value)
      Sets a Double property. Replaces any previous value for the given property name.
      Overrides:
      setDouble in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setFloat

      public void setFloat(String name, Float value)
      Sets a Float property. Replaces any previous value for the given property name.
      Overrides:
      setFloat in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setBoolean

      public void setBoolean(String name, Boolean value)
      Sets a Boolean value for the given property name. Replaces any previous value for the named property.
      Overrides:
      setBoolean in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setInteger

      public void setInteger(String name, Integer value)
      Sets an Integer property. Replaces any previous value for the given property name.
      Overrides:
      setInteger in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setString

      public void setString(String name, String value)
      Sets a String name/value pair. Replaces any previous value for the given name.
      Overrides:
      setString in class Properties
      Parameters:
      name - The property name.
      value - The property value.
    • setFont

      public void setFont(String name, Font font)
      Description copied from class: Properties
      Sets a Font value, replacing any previous value with that name. If value is null, ignore. Internally, Font objects are split into five properties:
      • font family name
      • font face name
      • is bold
      • is italic
      • point size
      These are transparently read back into a single Font instance by the getFont() method.
      Overrides:
      setFont in class Properties
      Parameters:
      name - The name of the property to set.
      font - The Font object to set.