Package ca.corbett.extras.properties
Class FileBasedProperties
java.lang.Object
ca.corbett.extras.properties.Properties
ca.corbett.extras.properties.FileBasedProperties
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
-
Field Summary
Fields inherited from class ca.corbett.extras.properties.Properties
props -
Constructor Summary
ConstructorsConstructorDescriptionFileBasedProperties(File file) Creates a new, empty FileBasedProperties pointed at the given File object. -
Method Summary
Modifier and TypeMethodDescriptionReturns the comment header block that was set in this FileBasedProperties.getFile()Returns the File around which this instance is based.booleanIndicates whether this FileBasedProperties instance has a comment header set.booleanReturns whether this instance is set to eagerly save changes or not.voidload()Attempts to load property values from our file.voidRemoves the property with the given name, if any.voidsave()Saves the current properties to our file.voidSaves the current properties to our file.voidAttempts to save without throwing an IOException -if one occurs, it will be logged without being rethrown.voidsaveWithoutException(String comments) Attempts to save without throwing an IOException - if one occurs, it will be logged without being rethrown.voidsetBoolean(String name, Boolean value) Sets a Boolean value for the given property name.voidSets a Color property.voidsetCommentHeader(String header) You can set an optional comment header that will be written out at the top of the file whenever save() is invoked.voidSets a Double property.voidsetEagerSave(boolean eager) Sets whether this object should save eagerly (default is false).voidSets a Float property.voidSets a Font value, replacing any previous value with that name.voidsetInteger(String name, Integer value) Sets an Integer property.voidSets a String name/value pair.Methods inherited from class ca.corbett.extras.properties.Properties
createFontFromAttributes, decodeColor, encodeColor, getBoolean, getColor, getDouble, getFloat, getFont, getInteger, getPropertyNames, getString, getString
-
Constructor Details
-
FileBasedProperties
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
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
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
Attempts to load property values from our file.- Throws:
IOException- If something goes wrong.
-
save
Saves the current properties to our file.- Throws:
IOException- If something goes wrong.
-
getFile
Returns the File around which this instance is based.- Returns:
- A File.
-
save
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
Removes the property with the given name, if any.- Overrides:
removein classProperties- 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
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
Sets a Color property. Replaces any previous value for the given property name.- Overrides:
setColorin classProperties- Parameters:
name- The property name.value- The property value.
-
setDouble
Sets a Double property. Replaces any previous value for the given property name.- Overrides:
setDoublein classProperties- Parameters:
name- The property name.value- The property value.
-
setFloat
Sets a Float property. Replaces any previous value for the given property name.- Overrides:
setFloatin classProperties- Parameters:
name- The property name.value- The property value.
-
setBoolean
Sets a Boolean value for the given property name. Replaces any previous value for the named property.- Overrides:
setBooleanin classProperties- Parameters:
name- The property name.value- The property value.
-
setInteger
Sets an Integer property. Replaces any previous value for the given property name.- Overrides:
setIntegerin classProperties- Parameters:
name- The property name.value- The property value.
-
setString
Sets a String name/value pair. Replaces any previous value for the given name.- Overrides:
setStringin classProperties- Parameters:
name- The property name.value- The property value.
-
setFont
Description copied from class:PropertiesSets 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
- Overrides:
setFontin classProperties- Parameters:
name- The name of the property to set.font- The Font object to set.
-