Share on Google+Share on Google+

Importing Preferences

This example demonstrates you how to import a file of exported preference data.

Importing Preferences


This example demonstrates you how to import a file of exported preference data. 

Preferences can be exported using the methods Preferences.exportNode() and Preferences.exportSubtree(). This exported data can be imported by using the method Preferences.importPreferences(). The preference in the exported data contains the path of the node that held the preference. When the exported preference is imported, it is added to the node with the same path. You can see in the given example that we have create an file input stream and by using the method importPreferences(is), we can import all of the preferences represented by the XML document on the specified input stream. To display the imported data on the console, we have used the method exportSubtree(System.out).

Here is the code of JavaTypes.xml 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE preferences SYSTEM "">
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<node name="Java Types">
<entry key="String" value="a string"/>
<entry key="Boolean" value="true"/>
<entry key="Integer" value="123"/>
<entry key="Long" value="123"/>
<entry key="Float" value="12.3"/>
<entry key="Double" value="12.3"/>

Here is the code of

import java.util.prefs.*;

public class ImportPreferences{
public static void main(String []args){
InputStream is = null;
 try {
  is = new 
new FileInputStream
 Preferences root = Preferences.userRoot();
 catch (Exception e) {

Output will be displayed as:

Download Source Code:



Posted on: January 12, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Importing Preferences  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image