In this section, we are going to retrieve the Parent and Child nodes of a Preference Node.
Retrieving the Parent and Child Nodes of a Preference Node
In this section, we are going to retrieve the Parent and Child nodes of a Preference Node. You can see in the given example that we have used Preferences.userNodeForPackage(java.lang.String.class) to get the Preference node. After that the method prefs.parent() returns the parent node. We are using the method prefs.childrenNames() in order to get the child node. If there is no child node corresponding to the Preference node, it will return null.
Here is the code of RetrievingParentAndChild.java
import java.io.*; import java.util.prefs.*; public class RetrievingParentAndChild{ public static void main(String[]args) throws Exception{ Preferences prefs = Preferences.userNodeForPackage(java.lang.String.class); Preferences node = prefs.parent(); System.out.println("Parent Node:"+node); node = node.parent(); String[] names = null; names = prefs.childrenNames(); for (int i=0; i<names.length; i++) { node = prefs.node(names[i]); } System.out.println("Child Node:"+node); } } |
Output will be displayed as: