Removing a Preference Node - Java Native OS

Java examples for Native OS:Preference

Description

Removing a Preference Node

Demo Code

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

public class Main {
  public static void main(String[] argv) throws Exception {
    try {//from  w ww .  ja v a 2 s .c  om
      boolean exists = Preferences.userRoot().nodeExists("/foo"); // false

      if (!exists) {
        // Get the node
        Preferences prefs = Preferences.userRoot().node("/foo");

        // Remove the node
        prefs.removeNode();

        // Trying to remove it again would cause an IllegalStateException
        // prefs.removeNode();
      }

      // Create a node with a child
      Preferences prefs = Preferences.userRoot().node("/foo/child");
      exists = Preferences.userRoot().nodeExists("/foo"); // true
      exists = Preferences.userRoot().nodeExists("/foo/child"); // true

      // Remove the parent node
      Preferences.userRoot().node("/foo").removeNode();

      // Both parent and child are removed
      exists = Preferences.userRoot().nodeExists("/foo"); // false
      exists = Preferences.userRoot().nodeExists("/foo/child"); // false
    } catch (BackingStoreException e) {
    }
  }
}

Result


Related Tutorials