![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e419. Exporting the Preferences in a Preference NodeThis example demonstrates how to export the preferences in a preference node to a file. The exported values can be imported usingPreferences.importPreferences() (see
e418 Importing Preferences). The format of the exported data
is XML.
The exported data contains the path of the node that was exported. When the data is later imported, the preferences will be added to the node with exactly the same path. // 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 prefs.putFloat("myFloat", 12.3F); // float prefs.putDouble("myDouble", 12.3); // double byte[] bytes = new byte[10]; prefs.putByteArray("myByteArray", bytes); // byte[] try { // Export the node to a file prefs.exportNode(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 /> <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" /> <entry key="myFloat" value="12.3" /> <entry key="myDouble" value="12.3" /> <entry key="myByteArray" value="AAAAAAAAAAAAAA==" /> </map> </node> </node> </root> </preferences>
e420. Exporting the Preferences in a Subtree of Preference Nodes
© 2002 Addison-Wesley. |