OnJava8-Examples/preferences/PreferencesDemo.java

42 lines
1.3 KiB
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// preferences/PreferencesDemo.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import java.util.prefs.*;
public class PreferencesDemo {
2016-01-25 18:05:55 -08:00
public static void
main(String[] args) throws Exception {
2015-06-15 17:47:35 -07:00
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())
2016-01-25 18:05:55 -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
2016-07-27 11:12:11 -06:00
UsageCount: 191
2015-06-15 17:47:35 -07:00
How many companions does Dorothy have? 4
2016-07-22 14:45:35 -06:00
___[ Error Output ]___
2016-07-27 11:12:11 -06:00
Jul 27, 2016 10:50:50 AM java.util.prefs.WindowsPreferences
2016-07-22 14:45:35 -06:00
<init>
WARNING: Could not open/create prefs root node
Software\JavaSoft\Prefs at root 0x80000002. Windows
RegCreateKeyEx(...) returned error code 5.
2015-09-07 11:44:36 -06:00
*/