Transcript Preferences

Preferences
Persistent storage


Information is persistent if it is kept between one
invocation of a program and the next
Many programs keep user preferences in a persistent
fashion



To keep persistent information,



Sometimes the user sets these explicitly (in, say, a dialog box)
Sometimes these are implicit settings, such as window size
and position
Write it to a file whenever it is changed (or when the program
quits), and
Read it in again whenever the program starts up
Java makes this very easy to do
2
Types of preferences

Java distinguishes user preferences, for a particular
user, and system preferences, for everybody


Each “program” may have its own preferences file



To create or change system preferences, you need
“administrator privileges”
Java saves and restores preferences for a particular class
Preferences are kept in a hierarchical tree structure
We will consider only the simplest version, in which
preference settings act like a hash table
3
Constructing a Preferences object





import java.util.prefs.*;
public class MyProgram { ... }
private Preferences userPrefs;
private Preferences systemPrefs;
userPrefs =
Preferences.userNodeForPackage(MyProgram.class);
systemPrefs =
Preferences.systemNodeForPackage(MyProgram.class);
Note that MyProgram.class returns the Class of MyProgram
4
Simple use of preferences



myPrefs.put(String key, String value)
 Just like putting a value into a hash table
 The call is the same for both user and system preferences
String value = myPrefs.get(String key, String default)
 Almost like getting a value from a hash table
 You must supply a default value, in case the key isn’t found
Saving and reloading preferences



You don’t—it all happens automatically
Java puts it in some system-dependent location (based on the Class you
gave it) that it knows about and can find again
Your preferences are “just there,” the next time you use preferences for
the same Class
5
Saving preferences

The following are methods of your Preferences object:







void put(String key, String value)
void putBoolean(String key, boolean value)
void putByteArray(String key, byte[] value)
void putDouble(String key, double value)
void putFloat(String key, float value)
void putInt(String key, int value)
void putLong(String key, long value)

void remove(String key) // remove this preference
void clear()
// remove all preferences

void sync()

// update preferences file now
6
Getting saved preferences


All getter methods must supply a default value, in case the
preferences files does not exist or cannot be read
The following are methods of your Preferences object:

String get(String key, String default)
boolean getBoolean(String key, boolean default)
byte[] getByteArray(String key, byte[] default)
double getDouble(String key, double default)
float getFloat(String key, float default)
int getInt(String key, int default)
long getLong(String key, long default)

String[] keys()






7
How does this work?

Changes to the Preferences object (via put or putXXX)
happen automatically--all file I/O is done for you


We have treated the preferences file as if it were a flat
file; however, it’s actually a tree structure




You only need sync() if you want to force the file update to happen
immediately rather than eventually
Use slashes (/) to separate parts, just as in a file path
If you want to know more, go to the API
The preferences file is in XML format
Java puts the preferences file in a system-dependent location on
your computer


You can export to/import from a particular file
You can view/edit this XML directly if you like
8
Final comments

As described in these slides, a Preferences object is
simply a hash table that is “magically” kept between
invocations of your program



As such, preferences are really easy to use
Preference files are really intended to be used for small
amounts of persistent data (for example, preferences!)
For large amounts of data, you really should use a
database instead
9
The End
10