By kswaughs | Monday, November 9, 2015

Java Preferences - Read, Write, Import and Export

1. To write user data into user preferences

Sample code to write user preferences
public void write() throws Exception {
  
 Preferences user1Prefs = Preferences.userRoot().node("user1");
  
 user1Prefs.put("name", "Srinivas K");
 user1Prefs.put("designation", "Manager");
}

2. To read user data from user preferences

Sample code to read user preferences
public void read() throws Exception {
  
 Preferences user1Prefs = Preferences.userRoot().node("user1");
  
 System.out.println("*** user 1 details ***");
  
 System.out.println(user1Prefs.get("name", "def name val"));
 System.out.println(user1Prefs.get("designation", "def designation val"));
 System.out.println(user1Prefs.get("salary", "def salary val"));
}
Output :
*** user 1 details ***
Srinivas K
Manager
def salary val

3. To export user preferences into a XML file

Sample code to export user preferences
public void exportToFile() throws Exception {
  
 Preferences user1Prefs = Preferences.userRoot().node("user1");
  
 // To export the preferences into an xml file.
 OutputStream stream = new FileOutputStream(
   "src/resources/xml/users.xml");
 user1Prefs.exportNode(stream);
}
Output XML File :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
  <root type="user">
    <map/>
    <node name="user1">
      <map>
        <entry key="name" value="Srinivas K"/>
        <entry key="designation" value="Manager"/>
      </map>
    </node>
  </root>
</preferences>

4. To import user preferences from a XML file

Sample code to import user preferences
public void importFromFile() throws Exception {
  
 FileInputStream fis = new FileInputStream("src/resources/xml/users.xml");
 Preferences.importPreferences(fis);
  
    // Lets check new user2 details 
 Preferences user2Prefs = Preferences.userRoot().node("user2");
  
 System.out.println("*** user 2 details ***");
 System.out.println(user2Prefs.get("name", "def name val"));
 System.out.println(user2Prefs.get("designation", "def designation val"));

 // Lets check previous user1 details 
 Preferences user1Prefs = Preferences.userRoot().node("user1");
 
 System.out.println("*** user 1 details ***");
 System.out.println(user1Prefs.get("name", "def name val"));
 System.out.println(user1Prefs.get("designation", "def designation val"));
}
Input XML File with both users:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
  <root type="user">
    <map/>
    <node name="user1">
      <map>
        <entry key="name" value="Srinivas K"/>
        <entry key="designation" value="Manager"/>
      </map>
    </node>
     <node name="user2">
      <map>
        <entry key="name" value="Praveen G"/>
        <entry key="designation" value="Sr Manager"/>
      </map>
    </node>
  </root>
</preferences>
Output :
*** user 2 details ***
Praveen G
Sr Manager
*** user 1 details ***
Srinivas K
Manager

Recommend this on


No comments:

Post a Comment