Export Preferences to XML file in Java


In order to export preferences to an XML file in Java, we need to use the exportSubtree() method. This method emits an XML document showing all of the preferences contained in this node and all of its descendants. This XML document provides an offline backup of the subtree rooted at the node.

Declaration − The java.util.prefs.Preferences.exportSubtree method is declared as follows −

public abstract void exportSubtree(OutputStream os) throws IOException, BackingStoreException

Let us see a program to export preferences to an XML file in Java −

import java.io.FileOutputStream;
import java.util.prefs.Preferences;
public class Example {
   public static void main(String args[]) throws Exception {
   Preferences pre = Preferences.userRoot();
      Preferences prefs = pre.node("Example");
      myPrefs.put("S", "s");
      myPrefs.put("A", "a");
      myPrefs.put("D", "d");
      FileOutputStream fos = new FileOutputStream("abc.xml");
      prefs.exportSubtree(fos);
      fos.close();
   }
}

The command window looks as follows −

An XML file is created in the bin −

Updated on: 25-Jun-2020

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements