Class UserPrefs
java.lang.Object
|
+----java.util.Dictionary
|
+----java.util.Hashtable
|
+----java.util.Properties
|
+----UserPrefs
- public class UserPrefs
- extends Properties
Simple, generic class to load up or save properties for an application.
(eg user preferences)
While reading in values, it will automatically convert from string format,
to your choice of int or boolean, if desired.
We assume that you want properties stored in a file named
$HOME/.yourappname, where $HOME represents the users "home directory"
-
UserPrefs(String)
- We try to read in a preferences file, from the user's "HOME"
directory.
-
getBool(String)
- look up property that is a boolean value
-
getBool(String, boolean)
-
-
getInt(String)
- look up property that is an int value
-
getInt(String, int)
- look up property that is an int value
-
getString(String)
- Gets named resource, as a string value.
-
getString(String, String)
- Gets named resource, as a string value.
-
getUserHome()
- The java spec guarantees that a "home" directory be
specified.
-
Save()
- save user preferences to default file.
-
setPref(String, String)
- way to directly set a preference.
UserPrefs
public UserPrefs(String appname)
- We try to read in a preferences file, from the user's "HOME"
directory. We base the name of the file, on the name of the
application we are in.
Use the getUserHome() call if you want to know what directory
this is in.
- Parameters:
- appname - name of application calling this class
getUserHome
public String getUserHome()
- The java spec guarantees that a "home" directory be
specified. We look it up for our own uses at initialization
If you want to do other things in the user's home dir,
this routine is an easy way to find out where it is.
This returns string that will have trailing fileseparator, eg "/")
so you can slap together a filename directly after it, and
not worry about that sort of junk.
setPref
public void setPref(String prefname,
String value)
- way to directly set a preference. You'll have to
do your own type conversion.
- Parameters:
- prefname - name of property
- value - string value of property
getString
public String getString(String name)
- Gets named resource, as a string value.
returns null if no such resource defined.
- Parameters:
- name - name of property
getString
public String getString(String name,
String defstr)
- Gets named resource, as a string value.
- Parameters:
- name - name of property
- defval - default value to remember and return , if
no existing property name found.
getInt
public int getInt(String name)
- look up property that is an int value
- Parameters:
- name - name of property
getInt
public int getInt(String name,
int defval)
- look up property that is an int value
- Parameters:
- name - name of property
- defval - default value to remember and return , if
no existing property name found.
getBool
public boolean getBool(String name)
- look up property that is a boolean value
- Parameters:
- name - name of property
- defval - default value to remember and return , if
no existing property name found.
getBool
public boolean getBool(String name,
boolean defval)
- Parameters:
- name - name of property
- defval - default value to remember and return , if
no existing property name found.
Save
public void Save() throws IOException
- save user preferences to default file. Duh.