Preferences Persistent storage n n Information is persistent

  • Slides: 9
Download presentation
Preferences

Preferences

Persistent storage n n Information is persistent if it is kept between one invocation

Persistent storage n n 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 n To keep persistent information, n n n 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 n n Java distinguishes user preferences, for a particular user, and

Types of preferences n n Java distinguishes user preferences, for a particular user, and system preferences, for everybody Each “program” may have its own preferences file n n n 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 n n n import java. util. prefs. *; public class

Constructing a Preferences object n n n import java. util. prefs. *; public class My. Program {. . . } private Preferences user. Prefs; private Preferences system. Prefs; user. Prefs = Preferences. user. Node. For. Package(My. Program. class); system. Prefs = Preferences. system. Node. For. Package(My. Program. class); Note that My. Program. class returns the Class of My. Program 4

Getting preference values n All getter methods must supply a default value, in case

Getting preference values n All getter methods must supply a default value, in case the preferences files does not exist or cannot be read n String get(String key, String default) boolean get. Boolean(String key, boolean default) byte[] get. Byte. Array(String key, byte[] default) double get. Double(String key, double default) float get. Float(String key, float default) int get. Int(String key, int default) long get. Long(String key, long default) n String[] keys() n n n 5

Setting preference values n n n n void void put(String key, String value) put.

Setting preference values n n n n void void put(String key, String value) put. Boolean(String key, boolean value) put. Byte. Array(String key, byte[] value) put. Double(String key, double value) put. Float(String key, float value) put. Int(String key, int value) put. Long(String key, long value) n void remove(String key) // remove this preference void clear() // remove all preferences n void sync() n // update preferences file now 6

How does this work? n Changes to the Preferences object (via put or put.

How does this work? n Changes to the Preferences object (via put or put. XXX) happen automatically--all file I/O is done for you n n We have treated the preferences file as if it were a flat file; however, it’s actually a tree structure n n n You only need sync() if you want to force the file update to happen immediately rather than eventually 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 n n You can export to/import from a particular file You can view/edit this XML directly if you like 7

Final comments n As described in these slides, a Preferences object is simply a

Final comments n As described in these slides, a Preferences object is simply a hash table that is “magically” kept between invocations of your program n n n As such, preferences are really easy to use Preference files are really intended to be used for small amounts of persistent data For large amounts of data, you really should use a database instead 8

The End 9

The End 9