Datasheet
Other Operations
The following table lists the other methods in the Preference class, such as writing any pending
changes to the backing store, resetting the preference hierarchy to empty, saving the hierarchy to disk,
and other operations.
Method Description
void clear() Removes all preferences on this node.
void exportNode Writes the entire contents of the node (and only the current node)
(OutputStream os) to the output stream as an XML file (following the preferences
.dtd
listed in the following section).
void exportSubtree Writes the entire contents of this node and all nodes located below
(OutputStream os) this node in the preferences tree to the output stream as an XML file
(following the
preferences.dtd listed in the following section).
void flush() Writes any changes to the preference node to the backing store,
including data on all children nodes.
void remove(String key) Removes the value associated with the specified key.
void sync() Ensures that the current version of the preference node in memory
matches that of the stored version. If data in the preference node
needs to be written to the backing store, it will be.
String toString() Returns a string containing User or System, depending on which
hierarchy the node is in, and the absolute path to the current node.
Exporting to XML
The Preferences system defines a standard operation to export the entire tree of keys/values to an
XML file. This XML file’s DTD is available at
http://java.sun.com/dtd/preferences.dtd. This
DTD is also included here:
<?xml version=”1.0” encoding=”UTF-8”?>
<!-- DTD for a Preferences tree. -->
<!-- The preferences element is at the root of an XML document
representing a Preferences tree. -->
<!ELEMENT preferences (root)>
<!-- The preferences element contains an optional version
attribute, which specifies version of DTD. -->
<!ATTLIST preferences EXTERNAL_XML_VERSION CDATA “0.0” >
<!-- The root element has a map representing the root’s preferences
(if any), and one node for each child of the root (if any). -->
<!ELEMENT root (map, node*) >
<!-- Additionally, the root contains a type attribute, which
specifies whether it’s the system or user root. -->
74
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 74