Removing a Preference from a Preference Node in Java

Java 8Object Oriented ProgrammingProgramming

In order to remove a preference from a preference node in Java, we use the remove() method. The remove method() removes all the values associated with the specified key in the preference node.

Declaration − The java.util.prefs.Preferences.remove() method is declared as follows −

public abstract void remove (String key)

where key is the key whose preference is to be removed

The remove methods throws the following exceptions −

NullPointerExceptionThis exception occurs when the key is null
IllegalStateExceptionThis exception is thrown when the ancestor node is removed by the removeNode() method.

Let us see a program to remove the preference from a preference node −

Example

 Live Demo

import java.util.prefs.Preferences;
public class Example {
   public static void main(String[] args) throws Exception {
      // obtains the user preference node for java.lang
      Preferences pre = Preferences.userNodeForPackage(String.class);
      // Remove a preference in the node
      final String name = "name";
      pre.remove(name);
   }
}

Output

Dec 26, 2018 6:00:21 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
raja
Updated on 25-Jun-2020 14:58:52

Advertisements