我创建了一个 Swing 应用程序 - GUI,其中包含文本字段、标签、复选框和组合框等字段。当用户输入一些信息时,我希望将文本字段、组合框和复选框的详细信息保存到一个文件中,下次用户打开此窗口时,我想要保存在文件中的详细信息,即那些用户上次输入的内容将被加载到 GUI 中。谁能帮我做这件事?我希望你能理解这个问题,如果没有,我会以更详细的方式解释。
非常感谢您。
最佳答案
这是一个使用 java.util.prefs 的简单示例包裹:
// Retrieve the user preference node for the package com.mycompany
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);
// Preference key name
final String PREF_NAME = "name_of_preference";
// Set the value of the preference
String newValue = "a string";
prefs.put(PREF_NAME, newValue);
// Get the value of the preference;
// default value is returned if the preference does not exist
String defaultValue = "default string";
String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string"
首选项的保存方式取决于操作系统。在 Windows 上,它将使用注册表。
使用示例:http://www.exampledepot.com/egs/java.util.prefs/pkg.html
关于java - java中的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037339/