Java 如何使用org.osgi.service.prefs.Preferences.clear()

Java 如何使用org.osgi.service.prefs.Preferences.clear(),java,eclipse,osgi,Java,Eclipse,Osgi,我正在尝试使用org.osgi.service.prefs.preferences加载和保存一些简单的首选项。第一次保存成功,但我在后续运行中所做的更改无法更改文件。看看这个和,我认为我做的是正确的步骤。在调试模式下运行时,在调用clear()之后,仍然可以看到相同的子键/值对。此外,刷新首选项后,磁盘上的文件不会更改。我是否必须调用flush()才能执行此操作?(我必须刷新到磁盘才能更改内存中的某些内容,这似乎很愚蠢——但这并没有帮助) 我做错了什么 下面是我保存描述符的代码(注意,这是McC

我正在尝试使用
org.osgi.service.prefs.preferences
加载和保存一些简单的首选项。第一次保存成功,但我在后续运行中所做的更改无法更改文件。看看这个和,我认为我做的是正确的步骤。在调试模式下运行时,在调用
clear()
之后,仍然可以看到相同的子键/值对。此外,刷新首选项后,磁盘上的文件不会更改。我是否必须调用
flush()
才能执行此操作?(我必须刷新到磁盘才能更改内存中的某些内容,这似乎很愚蠢——但这并没有帮助)

我做错了什么

下面是我保存描述符的代码(注意,这是McCaffer、Lemieux和Aniszczyk无耻地从“Eclipse富客户机平台”复制而来的,只是做了一些小修改,以更新Eclipse 3.8.1的API):

Preferences=ConfigurationScope.INSTANCE.getNode(Application.PLUGIN\u ID);
preferences.put(最后一个用户,connectionDetails.getUserId());
首选项连接=首选项.node(已保存);
试一试{
连接。清除();
//preferences.flush();
}捕获(备份存储例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
preferences=ConfigurationScope.INSTANCE.getNode(Application.PLUGIN\u ID);
连接=首选项.node(已保存);
for(Iterator it=savedDetails.keySet().Iterator();it.hasNext();){
String name=it.next();
ConnectionDetails d=(ConnectionDetails)savedDetails.get(name);
首选项连接=连接。节点(名称);
connection.put(SERVER,d.getServer());
connection.put(PASSWORD,d.getPassword());
}
试一试{
preferences.flush();
}捕获(备份存储例外){
e、 printStackTrace();
}       

必须刷新首选项才能正确应用修改,即必须调用
flush()
。有些操作可能会自动刷新,但这是一个不应该依赖的实现细节。另外,
clear()
仅删除选定节点上的关键点。要删除节点及其所有子节点,必须调用
removeNode()

// get node "config/plugin.id"
// note: "config" identifies the configuration scope used here
final Preferences preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");

// set key "a" on node "config/plugin.id"
preferences.put("a", "value");

// get node "config/plugin.id/node1"
final Preferences connections = preferences.node("node1");

// remove all keys from node "config/plugin.id/node1"
// note: this really on removed keys on the selected node
connections.clear();

// these calls are bogous and not necessary
// they get the same nodes as above
//preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");
//connections = preferences.node("node1");

// store some values to separate child nodes of "config/plugin.id/node1"
for (Entry<String, ConnectionDetails> e : valuesToSave.entrySet()) {
    String name = e.getKey();
    ConnectionDetails d = e.getValue();
    // get node "config/plugin.id/node1/<name>"
    Preferences connection = connections.node(name);
    // set keys "b" and "c"
    connection.put("b", d.getServer());
    connection.put("c", d.getPassword());
}

// flush changes to disk (if not already happend)
// note: this is required to make sure modifications are persisted
// flush always needs to be called after making modifications
preferences.flush();
//获取节点“config/plugin.id”
//注意:“配置”标识此处使用的配置范围
最终首选项=ConfigurationScope.INSTANCE.getNode(“plugin.id”);
//在节点“config/plugin.id”上设置键“a”
优先权。看跌期权(“a”、“价值”);
//获取节点“config/plugin.id/node1”
最终首选项连接=Preferences.node(“node1”);
//从节点“config/plugin.id/node1”中删除所有密钥
//注意:这实际上取决于选定节点上已删除的关键点
连接。清除();
//这些电话是假的,没有必要
//它们得到与上面相同的节点
//preferences=ConfigurationScope.INSTANCE.getNode(“plugin.id”);
//连接=首选项。节点(“节点1”);
//存储一些值以分离“config/plugin.id/node1”的子节点
对于(条目e:valuesToSave.entrySet()){
字符串名称=e.getKey();
ConnectionDetails d=e.getValue();
//获取节点“config/plugin.id/node1/”
首选项连接=连接。节点(名称);
//设置键“b”和“c”
connection.put(“b”,d.getServer());
connection.put(“c”,d.getPassword());
}
//刷新对磁盘的更改(如果尚未发生)
//注意:这是确保保存修改所必需的
//修改后始终需要调用flush
preferences.flush();

removeNode成功了。最终起作用的是在final preferences connections=preferences.node(“node1”)行之前添加preferences.node(“node1”).removeNode();谢谢,古纳。
// get node "config/plugin.id"
// note: "config" identifies the configuration scope used here
final Preferences preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");

// set key "a" on node "config/plugin.id"
preferences.put("a", "value");

// get node "config/plugin.id/node1"
final Preferences connections = preferences.node("node1");

// remove all keys from node "config/plugin.id/node1"
// note: this really on removed keys on the selected node
connections.clear();

// these calls are bogous and not necessary
// they get the same nodes as above
//preferences = ConfigurationScope.INSTANCE.getNode("plugin.id");
//connections = preferences.node("node1");

// store some values to separate child nodes of "config/plugin.id/node1"
for (Entry<String, ConnectionDetails> e : valuesToSave.entrySet()) {
    String name = e.getKey();
    ConnectionDetails d = e.getValue();
    // get node "config/plugin.id/node1/<name>"
    Preferences connection = connections.node(name);
    // set keys "b" and "c"
    connection.put("b", d.getServer());
    connection.put("c", d.getPassword());
}

// flush changes to disk (if not already happend)
// note: this is required to make sure modifications are persisted
// flush always needs to be called after making modifications
preferences.flush();