在java中的配置文件 [英] Configuration Files in java
问题描述
非常感谢你提前。
下面是一个简单的例子,使用 java.util.prefs 包:
//检索包的用户首选项节点com.mycompany
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);
//首选键名称
最终字符串PREF_NAME =name_of_preference;
//设置首选项的值
String newValue =a string;
prefs.put(PREF_NAME,newValue);
//获取首选项的值;
//如果首选项不存在,则返回默认值
String defaultValue =default string;
String propertyValue = prefs.get(PREF_NAME,defaultValue); //a string
保存偏好的方式取决于操作系统。在Windows上,它将使用注册表。
使用示例:http://www.exampledepot.com/egs/java.util.prefs/pkg.html
I have created a Swing Application- GUI containing fields like TextFields, Labels, CheckBoxes and ComboBoxes. When the user enters some information, I want the details of the textfields, comboboxes and checkboxes to be saved into a file, and the next time when a user opens this Window, I want the details that have been saved in the file, i.e. those that have been entered by the user the previous time to be loaded into the GUI. Can anyone please help me in doing this? I hope you understand the question, if not I will explain in a more detailed manner.
Thank you so much in advance.
Here is a simple example using the java.util.prefs package:
// 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"
The way preferences are saved is OS-dependent. On Windows, it will use the registry.
Examples of use: http://www.exampledepot.com/egs/java.util.prefs/pkg.html
这篇关于在java中的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!