无法将布尔首选项从 Java 存储到 Windows 10 注册表,而整数和字符串已正确存储
Impossible to store boolean preferences to Windows 10 registry from Java, while integer and String are correctly stored
我正在使用 https://www.vogella.com/tutorials/JavaPreferences/article.html 中的示例和以下代码:
import java.util.prefs.Preferences;
public class PreferenceTest {
private Preferences prefs;
public void setPreference() {
// This will define a node in which the preferences can be stored
prefs = Preferences.userRoot().node(this.getClass().getName());
String ID1 = "Test1";
String ID2 = "Test2";
String ID3 = "Test3";
// First we will get the values
// Define a boolean value
System.out.println(prefs.getBoolean(ID1, true));
// Define a string with default "Hello World
System.out.println(prefs.get(ID2, "Hello World"));
// Define a integer with default 50
System.out.println(prefs.getInt(ID3, 50));
// now set the values
prefs.putBoolean(ID1, false);
prefs.put(ID2, "Hello Europa");
prefs.putInt(ID3, 45);
// Delete the preference settings for the first value
prefs.remove(ID1);
}
public static void main(String[] args) {
PreferenceTest test = new PreferenceTest();
test.setPreference();
}
}
及以下是后续 运行s:
的输出
1)初始运行保存首选项:
真
2019 年 4 月 23 日 10:56:22 下午 java.util.prefs.WindowsPreferences
你好世界
警告:无法 open/create 在根 0x80000002 处优先选择根节点 Software\JavaSoft\Prefs。 Windows RegCreateKeyEx(...) 返回错误代码 5。
50
2) 第二个 运行 已经读取了第一个 运行 写入的值:
真
2019 年 4 月 23 日 10:56:57 下午 java.util.prefs.WindowsPreferences
你好欧罗巴
警告:无法 open/create 在根 0x80000002 处优先选择根节点 Software\JavaSoft\Prefs。 Windows RegCreateKeyEx(...) 返回错误代码 5。
45
您可能会注意到 int 和 String 值被很好地检索并且没有被默认值替换,而布尔值没有被检索并且被替换为默认值(即两个输出都给出 true 而期望是在第二个 运行).
实现 false
我在这里错了:我错过了 ID1 密钥已从首选项中删除的事实:
// Delete the preference settings for the first value
prefs.remove(ID1);
如果我将其注释掉,一切都会按预期进行。
我正在使用 https://www.vogella.com/tutorials/JavaPreferences/article.html 中的示例和以下代码:
import java.util.prefs.Preferences;
public class PreferenceTest {
private Preferences prefs;
public void setPreference() {
// This will define a node in which the preferences can be stored
prefs = Preferences.userRoot().node(this.getClass().getName());
String ID1 = "Test1";
String ID2 = "Test2";
String ID3 = "Test3";
// First we will get the values
// Define a boolean value
System.out.println(prefs.getBoolean(ID1, true));
// Define a string with default "Hello World
System.out.println(prefs.get(ID2, "Hello World"));
// Define a integer with default 50
System.out.println(prefs.getInt(ID3, 50));
// now set the values
prefs.putBoolean(ID1, false);
prefs.put(ID2, "Hello Europa");
prefs.putInt(ID3, 45);
// Delete the preference settings for the first value
prefs.remove(ID1);
}
public static void main(String[] args) {
PreferenceTest test = new PreferenceTest();
test.setPreference();
}
}
及以下是后续 运行s:
的输出1)初始运行保存首选项:
真 2019 年 4 月 23 日 10:56:22 下午 java.util.prefs.WindowsPreferences 你好世界 警告:无法 open/create 在根 0x80000002 处优先选择根节点 Software\JavaSoft\Prefs。 Windows RegCreateKeyEx(...) 返回错误代码 5。 50
2) 第二个 运行 已经读取了第一个 运行 写入的值:
真 2019 年 4 月 23 日 10:56:57 下午 java.util.prefs.WindowsPreferences 你好欧罗巴 警告:无法 open/create 在根 0x80000002 处优先选择根节点 Software\JavaSoft\Prefs。 Windows RegCreateKeyEx(...) 返回错误代码 5。 45
您可能会注意到 int 和 String 值被很好地检索并且没有被默认值替换,而布尔值没有被检索并且被替换为默认值(即两个输出都给出 true 而期望是在第二个 运行).
实现 false我在这里错了:我错过了 ID1 密钥已从首选项中删除的事实:
// Delete the preference settings for the first value
prefs.remove(ID1);
如果我将其注释掉,一切都会按预期进行。