e419. exporting the preferences in a preference node.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 49 行

TXT
49
字号
This example demonstrates how to export the preferences in a preference node to a file. The exported values can be imported using Preferences.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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?