Determining If a Preference Node Exists - Java Native OS

Java examples for Native OS:Preference

Description

Determining If a Preference Node Exists

Demo Code

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class Main {
  public static void main(String[] args) {
    try {/*from w  w  w.j a  v a  2  s  .c o m*/
      // 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) {
    }
  }
}

Result


Related Tutorials