Preferences对象始终代表整个Preferences树中的特定节点,如下所示:
/userRoot├── com│ └── mycompany│ └── myapp│ ├── darkApplicationMode=true │ ├── showExitConfirmation=false │ └── windowMaximized=true └── org └── myorganization └── anotherapp ├── defaultFont=Helvetica ├── defaultSavePath=/home/matt/Documents └── exporting ├── defaultFormat=pdf └── openInBrowserAfterExport=false
要选择/com/mycompany/myapp节点:
按照惯例,基于一个类的包:
package com.mycompany.myapp;
// ...
// 由于此类位于com.mycompany.myapp包中,因此该节点
// / com / mycompany / myapp将返回。
Preferences myApp = Preferences.userNodeForPackage(getClass());
通过相对路径:
Preferences myApp = Preferences.userRoot().node("com/mycompany/myapp");
使用相对路径(不是以开头的路径/)将导致该路径相对于在其上解析的父节点进行解析。例如,以下示例将返回path的节点/one/two/three/com/mycompany/myapp:
Preferences prefix = Preferences.userRoot().node("one/two/three");
Preferences myAppWithPrefix = prefix.node("com/mycompany/myapp");
// prefix is /one/two/three
// myAppWithPrefix是/一/二/三/ com / mycompany / myapp
通过绝对路径:
Preferences myApp = Preferences.userRoot().node("/com/mycompany/myapp");
在根节点上使用绝对路径与使用相对路径没有什么不同。区别在于,如果在子节点上调用,则将相对于根节点解析路径。
Preferences prefix = Preferences.userRoot().node("one/two/three");
Preferences myAppWitoutPrefix = prefix.node("/com/mycompany/myapp");
// prefix is /one/two/three
// myAppWitoutPrefix是/ com / mycompany / myapp