Java如何清除kidCache?
Java类Java如何清除kidCache?,java,preferences,Java,Preferences,Java类首选项有一个名为kidCache的HashMap属性。执行Preferences方法remove(字符串节点)时,节点将有效地从用户的首选项文件中删除,但不会从kidCache属性中删除。 因此,当我打印到控制台prefs.nodeExists(“account-1”)或Arrays.toString(prefs.childrenNames())时,它仍然显示为可用。它只有在当前程序完成执行后才最终清除 public class ClassA { private Prefere
首选项
有一个名为kidCache
的HashMap属性。执行Preferences
方法remove(字符串节点)
时,节点将有效地从用户的首选项文件中删除,但不会从kidCache
属性中删除。因此,当我打印到控制台
prefs.nodeExists(“account-1”)
或Arrays.toString(prefs.childrenNames())
时,它仍然显示为可用。它只有在当前程序完成执行后才最终清除
public class ClassA {
private Preferences prefs = Preferences.userRoot().node("TestNode");
public static void main(String[] args) {
new ClassA();
}
public ClassA() {
prefs = Preferences.userRoot().node("TestNode/account-1");
prefs.put("username", "testUser1");
prefs = Preferences.userRoot().node("TestNode");
prefs.remove("account-1/"); // node removed here
try {
System.out.println(prefs.nodeExists("account-1")); // a test for its existance still outputs 'true'
System.out.println(Arrays.toString(prefs.childrenNames())); // kidCache can be retrieved with this method
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
}
输出:
Connected to the target VM, address: '127.0.0.1:58572', transport: 'socket'
true
[account-1]
我尝试使用
prefs.flush()
,但没有成功。如何在程序完成执行之前完全清除子节点?我找到了解决问题的方法。虽然我还没有弄清楚如何手动清除kidCache,但是删除节点的正确方法是设置节点的路径,然后使用removeNode()
方法,而不是一级一级地使用remove(String node)
方法
public class ClassA {
private Preferences prefs = Preferences.userRoot().node("TestNode");
public static void main(String[] args) {
new ClassA();
}
public ClassA() {
prefs = Preferences.userRoot().node("TestNode/account-1");
prefs.put("username", "testUser1");
prefs.removeNode();
prefs = Preferences.userRoot().node("TestNode");
try {
System.out.println(prefs.nodeExists("account-1")); // a test for its existence now outputs 'false'
} catch (BackingStoreException e) {
e.printStackTrace();
}
}
}
输出
Connected to the target VM, address: '127.0.0.1:58572', transport: 'socket'
false
您可能正在寻找该方法吗?否sync()
不起作用。