Determine if a Preference Node exists in Java


In order to determine the existence a preference node in Java, we use the nodeExists() method. The nodeExists() method returns a boolean value. It returns true when the specified preference node exists in the same tree as this node.

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

public abstract boolean nodeExists(String pathname)throws BackingStoreException

where pathname is the path name of the node whose existence needs to be determined.

Let us see a program to determine if a preference node exists in Java −

Example

 Live Demo

import java.util.prefs.Preferences;
public class Example {
   public static void main(String[] args) throws Exception {
      boolean exist = Preferences.userRoot().nodeExists("/node");
      System.out.println("Checking node existence before creation: "+exist);
      Preferences.userRoot().node("/node");
      exist = Preferences.userRoot().nodeExists("/node");
      System.out.println("Checking node existence after creation: "+exist);
   }
}

Output

Checking node existence before creation: false
Checking node existence after creation: true
Dec 26, 2018 7:12:10 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.

Updated on: 25-Jun-2020

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements