-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPreferencesDemo.java
More file actions
42 lines (34 loc) · 1.09 KB
/
PreferencesDemo.java
File metadata and controls
42 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package io;
import java.io.*;
import java.util.*;
import java.util.prefs.*;
/**
* RUN:
* javac io/PreferencesDemo.java && java io.PreferencesDemo
*
* OUTPUT:
* Location: Oz
* Footwear: Ruby Slippers
* Companions: 4
* Are there witches?: true
* UsageCount: 1
* How many companions does Doroty have? 4
*/
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));
}
System.out.println("How many companions does Doroty have? "+
prefs.getInt("Companions", 0)
);
}
}