Java中的Preferences的基本使用方法:
Java代码 


import java.util.prefs.Preferences;
public class PreferencesDemo {
public static void main(String[] args) throws Exception {
Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class);
prefs.put("Location", "Oz");
prefs.put("Footwear", "Ruby Slippers");
prefs.putInt("Companions", 4);
prefs.putBoolean("Are there witches?", true);
int usageCount = prefs.getInt("UsageCount",
0);
usageCount++;
prefs.putInt("UsageCount", usageCount);
for (String key : prefs.keys()) {
System.out.println(key + ": " + prefs.get(key, null));
}
// You must always provide a default value:
System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions",
0));
}
}
import java.util.prefs.Preferences;
public class PreferencesDemo {
public static void main(String[] args) throws Exception {
Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class);
prefs.put("Location", "Oz");
prefs.put("Footwear", "Ruby Slippers");
prefs.putInt("Companions", 4);
prefs.putBoolean("Are there witches?", true);
int usageCount = prefs.getInt("UsageCount", 0);
usageCount++;
prefs.putInt("UsageCount", usageCount);
for (String key : prefs.keys()) {
System.out.println(key + ": " + prefs.get(key, null));
}
// You must always provide a default value:
System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions", 0));
}
}
代码来自:《Thinking in Java》
详细使用,请参考对应的JDK文档!=^_^=
本文详细介绍了如何在Java中使用Preferences类进行存储和获取配置信息,包括put、getInt、get等方法的应用,以及在《Thinking in Java》中的具体实践。通过阅读本文,开发者将了解如何处理用户设置和首选项的持久化存储。
77

被折叠的 条评论
为什么被折叠?



