Class FilePreferences
A file-based preferences implementation without length restrictions, on keys, values or node names.
Supports hierarchical preferences through nested JSON structure.
Clearing the preferences instance and flushing deletes the underlying file.
A value is returned as it was put, with one exception: a value that is an empty JSON object, {},
is not written to the file, nor is a node holding no values, so that neither is there once the file is read again.
A JSON value is stored nested in the file and may come back with its members in another order.
A key and a child node can not share a name, both being stored in the same JSON object. Putting a value
for a key naming a child node throws IllegalStateException, as does putting a value into a child node
named after a key, while AbstractPreferences.get(String, String) and AbstractPreferences.remove(String) ignore child nodes.
Note that, unlike the AbstractPreferences contract, PreferenceChangeListeners
are not notified of put(String, String) calls.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PropertyValue<String> Provides a way to override the default (OS dependent) directory used to store the user preferences file.static final PropertyValue<Boolean> Specifies whether JSON preference files are pretty-printed, causes significant size increase.Fields inherited from class AbstractPreferences
lock, newNodeFields inherited from class Preferences
MAX_KEY_LENGTH, MAX_NAME_LENGTH, MAX_VALUE_LENGTH -
Method Summary
Modifier and TypeMethodDescriptionString[]protected String[]protected AbstractPreferencesstatic PreferencesfilePreferences(String filename) voidflush()Saves the changes made since the last flush or sync, once for the whole tree, re-applied on top of the file if it has been modified externally in the meantime, assync()does.protected voidflushSpi()protected @Nullable AbstractPreferencesprotected String[]keysSpi()AsAbstractPreferences.node(String), without the limit on the length of a node name.voidprotected voidvoidAsAbstractPreferences.removeNode(), removing the long named nodes below this one as well.protected voidprotected voidvoidsync()Reloads the file if it has been modified externally since it was last read or written, re-applying the changes made since the last flush or sync on top of it, and saves those changes, once for the whole tree.protected voidsyncSpi()Methods inherited from class AbstractPreferences
absolutePath, addNodeChangeListener, addPreferenceChangeListener, cachedChildren, clear, exportNode, exportSubtree, get, getBoolean, getByteArray, getDouble, getFloat, getInt, getLong, isRemoved, isUserNode, keys, name, nodeExists, parent, putBoolean, putByteArray, putDouble, putFloat, putInt, putLong, remove, removeNodeChangeListener, removePreferenceChangeListener, toStringMethods inherited from class Preferences
importPreferences, systemNodeForPackage, systemRoot, userNodeForPackage, userRoot
-
Field Details
-
PREFERENCES_LOCATION
Provides a way to override the default (OS dependent) directory used to store the user preferences file.Note that changing this value after a file based preferences instance has been created has no effect on subsequent instances.
- Value type: String
- Default value: null
- See Also:
-
PRETTY_PRINT
Specifies whether JSON preference files are pretty-printed, causes significant size increase.Note that this value is captured when a file based preferences instance is created; changing it afterwards has no effect on that instance.
- Value type: Boolean
- Default value: false
-
-
Method Details
-
put
- Overrides:
putin classAbstractPreferences
-
filePreferences
- Parameters:
filename- the preferences filename- Returns:
- a file based Preferences instance based on the given filename
-
node
AsAbstractPreferences.node(String), without the limit on the length of a node name.- Overrides:
nodein classAbstractPreferences
-
childrenNames
- Overrides:
childrenNamesin classAbstractPreferences- Throws:
BackingStoreException
-
removeNode
AsAbstractPreferences.removeNode(), removing the long named nodes below this one as well.- Overrides:
removeNodein classAbstractPreferences- Throws:
BackingStoreException
-
getChild
- Overrides:
getChildin classAbstractPreferences- Throws:
BackingStoreException
-
putSpi
- Specified by:
putSpiin classAbstractPreferences
-
getSpi
- Specified by:
getSpiin classAbstractPreferences
-
removeSpi
- Specified by:
removeSpiin classAbstractPreferences
-
keysSpi
- Specified by:
keysSpiin classAbstractPreferences- Throws:
BackingStoreException
-
flush
Saves the changes made since the last flush or sync, once for the whole tree, re-applied on top of the file if it has been modified externally in the meantime, assync()does. Leaves the file untouched when there are no changes to save.AbstractPreferences.flush()would callflushSpi()on this node and on each cached descendant, saving the file once per node.- Overrides:
flushin classAbstractPreferences- Throws:
BackingStoreException- in case of an I/O error
-
sync
Reloads the file if it has been modified externally since it was last read or written, re-applying the changes made since the last flush or sync on top of it, and saves those changes, once for the whole tree.AbstractPreferences.sync()would callsyncSpi()on this node and on each cached descendant.- Overrides:
syncin classAbstractPreferences- Throws:
BackingStoreException- in case of an I/O errorIllegalStateException- in case this node has been removed
-
flushSpi
- Specified by:
flushSpiin classAbstractPreferences- Throws:
BackingStoreException
-
syncSpi
- Specified by:
syncSpiin classAbstractPreferences- Throws:
BackingStoreException
-
childrenNamesSpi
- Specified by:
childrenNamesSpiin classAbstractPreferences
-
childSpi
- Specified by:
childSpiin classAbstractPreferences
-
removeNodeSpi
protected void removeNodeSpi()- Specified by:
removeNodeSpiin classAbstractPreferences
-