![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e420. Exporting the Preferences in a Subtree of Preference NodesThis example demonstrates how to export the preferences of nodes in a subtree of preference nodes to a file. The export values can be imported usingPreferences.importPreferences() (see
e418 Importing Preferences). The format of the exported data
is XML.
For every node in the exported subtree, the path of the node and the preferences in that node are exported. When the data is later imported, the preferences are added to nodes with exactly the same paths. // Retrieve the user preference node for the package java.lang Preferences prefs = Preferences.userNodeForPackage(String.class); // Save some values prefs.put("myString", "a string"); // String prefs.putBoolean("myBoolean", true); // boolean prefs.putInt("myInt", 123); // int prefs.putLong("myLong", 123L); // long // Save some values in the parent node prefs = prefs.parent(); prefs.putFloat("myFloat", 12.3F); // float prefs.putDouble("myDouble", 12.3); // double byte[] bytes = new byte[10]; prefs.putByteArray("myByteArray", bytes); // byte[] try { prefs.exportSubtree(new FileOutputStream("output.xml")); } catch (IOException e) { } catch (BackingStoreException e) { }The code above generates the following XML data: <?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="java"> <map> <entry key="myFloat" value="12.3" /> <entry key="myDouble" value="12.3" /> <entry key="myByteArray" value="AAAAAAAAAAAAAA==" /> </map> <node name="lang"> <map> <entry key="myString" value="a string" /> <entry key="myBoolean" value="true" /> <entry key="myInt" value="123" /> <entry key="myLong" value="123" /> </map> </node> </node> </root> </preferences>
e419. Exporting the Preferences in a Preference Node
© 2002 Addison-Wesley. |