Export Preferences to XML file in Java

Java 8Object Oriented ProgrammingProgramming

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 −

raja
Published on 08-Jan-2019 11:48:11
Advertisements