OnJava8-Examples/preferences/PreferencesDemo.java

32 lines
964 B
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// preferences/PreferencesDemo.java
2015-11-14 16:18:05 -08:00
// <20>2016 MindView LLC: see Copyright.txt
2015-06-15 17:47:35 -07:00
import java.util.prefs.*;
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())
2015-11-03 12:00:44 -08:00
System.out.println(key + ": "+ prefs.get(key, null));
2015-06-15 17:47:35 -07:00
// You must always provide a default value:
2015-11-03 12:00:44 -08:00
System.out.println(
"How many companions does Dorothy have? " +
2015-06-15 17:47:35 -07:00
prefs.getInt("Companions", 0));
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Location: Oz
Footwear: Ruby Slippers
Companions: 4
Are there witches?: true
UsageCount: 33
How many companions does Dorothy have? 4
2015-09-07 11:44:36 -06:00
*/