Preferences : Java Glossary

java.util.prefs.Preferences first appeared in Java 1.4.1 as way to store configuration data for applications
and individual users.

Advantages of Preferences

Your app does not need to locate disk space to store them. Java does that for
you.

Your app does not need a mechanism to find the data again on disk. Java does
that for you.

You can store complex hierarchical configuration data with it.

It is designed so your names won’t collide with anyone else’s.

Disadvantages of Preferences

The preferences mechanism cannot be counted on to preserve the order of your
data.

Though you can embed dots in the key names to create a tree structure, there is
no support for arrays or keys with multiple values. If you have unique values in
your array, you can use them as keys. You can, however, get a list of all keys
under a node with keys(), You might use key/value pairs
like this
The problem with that scheme is insert and delete would be clumsy since you would
have to renumber all the keys and resave them, after removing the old keys. The
lines are limited to 8192, so you are limited to the number of elements with the
composite technique.

Another technique would be to serialize with a ByteArrayOutputStream ⇒ ObjectOutputStream and use putByteArray. The problem with that method is the registry would
be unintelligible (it is encoded in Base64) and you could not do manual patching. The other catch
is keys are limited to 80 bytes and values to a mere 8K.

The registry is fragile. It is hard to back up and restore. You can’t
restore at all if it is badly damaged. There is good chance you will eventually
lose everything. the Preferences class has methods to
import and export the preferences to *.xml files.

If they become corrupted, there is no easy way for an end user to view, edit or
delete them.

Use

systemNodeForPackage lets you specify Preferences for
the whole machine for each package. userNodeForPackage
lets you specify Preferences for a particular user for that package. If the package
is used in more than one context, e.g. the same class used in two different programs
or the same class is used to manage two separate sets of files, it is up to you to
create another level of name to keep track of the context. Note though you specify a
class name, the Preferences are on a per package, not a per class basis.

The registry will be more legible with regedit if you
stick to lower case keys and values since upper case values are encoded in a bizarre
fluffy way.

Location

In W98, Me, NT, W2K, XP, W2003, Vista, W2008, W7-32, W7-64, W8-32, W8-64, W2012, W10-32 and W10-64
this information is stored in the fragile, hard-to-back-up registry in HKEY_LOCAL_MACHINE\JavaSoft\Prefs for system
Preferences and HKEY_CURRENT_USER\JavaSoft\Prefs for user
Preferences in a very fluffy format. Every capital letter is preceded with a
/ and any fields containing accented letters are encoded in Base64.

In Windows, user Preferences show up at HKEY_CURRENT_USER\Software\JavaSoft\Prefs\com\mindprod\replicator
and HKEY_USERS\
usernamexxx\Software\JavaSoft\Prefs\com\mindprod\replicator where
the package name is com.mindprod.replicator.

In Windows, system Preferences show up at
HKEY_LOCAL_MACHINE\Software\JavaSoft\Prefs\com\mindprod\replicator,
where the package name is com.mindprod.replicator

In Linux, preferences are stored in ordinary xml files. System Preferences are stored in etc/.java.

In Linux, user preferences are stored in
~/.java. The file for user preferences may have a goofy
base64-encoded name something like this:

In theory, a JVM (Java Virtual Machine) could hide them in an SQL (Standard Query Language)
database or any other scheme it concocted.

Tips

Remember to call flush when you have finished
changing your Preferences to ensure they get written to backing store.

Alternatives include writing data to a server, storing it in an
SQL database,
storing in a POD (Persistent Object Database) and storing in ini, CSV (Comma-Separated Value),
DataOutputStream or serialised disk files.

Learning More

The Replicator uses the Preferences mechanism to persist configurations option for the client
as does Submitter and
VerCheck. You can examine
the source code to see a real world application.