在java中的配置文件 [英] Configuration Files in java

查看:143
本文介绍了在java中的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Swing应用程序 - 包含TextFields,Labels,CheckBoxes和ComboBoxes等字段的GUI。当用户输入一些信息时,我想将文本框,组合框和复选框的细节保存到一个文件中,下一次当用户打开这个窗口时,我想要保存在文件中的细节,已经由用户输入以前被加载到GUI中。任何人都可以帮助我这样做吗?我希望你明白这个问题,如果不是的话,我会以更详细的方式解释。

非常感谢你提前。

解决方案

下面是一个简单的例子,使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆