The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.prefs  [18 examples] > Nodes  [7 examples]

e415. Determining If a Preference Node Exists

A preference node is automatically created whenever Preferences.node(), Preferences.userNodeForPackage(), or Preferences.systemNodeForPackage() is called. To avoid creating a node, you should first check to see if it exists.
    try {
        // Check if a node exists
        boolean exists = Preferences.userRoot().nodeExists("/foo"); // false
    
        // Get the node
        Preferences.userRoot().node("/foo");
    
        // Getting a non-existent node automatically creates it
        exists = Preferences.userRoot().nodeExists("/foo"); // true
    
        // Remove the node
        Preferences prefs = Preferences.userRoot().node("/foo");
        prefs.removeNode();
    
        // The following would cause an IllegalStateException
        //exists = prefs.nodeExists("/foo");
    
        // Use the following to determine if the node has been removed
        exists = prefs.nodeExists(""); // false
    } catch (BackingStoreException e) {
    }

 Related Examples
e411. Getting the Roots of the Preference Trees
e412. Creating a Preference Node
e413. Retrieving a Preference Node
e414. Removing a Preference Node
e416. Retrieving the Parent and Child Nodes of a Preference Node
e417. Finding a Preference in a Preference Tree

See also: Events    Importing and Exporting   


© 2002 Addison-Wesley.