Determining If a Preference Node Contains a Specific Key
This section demonstrates you to determine whether the Preference Node contains a Specific key or not. You can see in the given example that we have create a method contain(String key) of Boolean type. This method returns true if the node contains the specified key otherwise it returns false. In order to get the Preference node, we have used Preferences.userRoot().node("/Roseindia Employees").
Here is the code of SpecificKey.java
import java.io.*; import java.util.prefs.*; public class SpecificKey{ public static boolean contain(String key) { Preferences node = Preferences.userRoot().node("/Roseindia Employees"); return node.get(key, null) != null; } public static void main(String[]args){ SpecificKey s=new SpecificKey(); boolean b=s.contain("EmployeeName1"); System.out.println(b); } } |
When you run the above program, the method search the specified key 'EmployeeName 1' in the Preference node 'Roseindia Employees'. If it finds, it will return true otherwise false. Here, the condition is true.