Download presentation
Presentation is loading. Please wait.
1
Preferences
2
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 Sometimes the user sets these explicitly (in, say, a dialog box) Sometimes these are implicit settings, such as window size and position To keep persistent information, 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
3
Types of preferences Java distinguishes user preferences, for a particular user, and system preferences, for everybody Each “program” may have its own preferences file 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
4
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
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 You only need sync() if you want to force the file update to happen immediately rather than eventually We have treated the preferences file as if it were a flat file; however, it’s actually a tree structure 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.