Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Remove Preference from a Preference Node in Java



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 −

NullPointerException This exception occurs when the key is null
IllegalStateException This 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.
Updated on: 2020-06-25T14:58:52+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements