Store Objects Using the JDK 1.4 Preferences API
An anonymous reader writes "The Preferences API -- a lightweight, cross-platform persistence API introduced in JDK 1.4 -- is designed to store small amounts of data (string, simple byte arrays, and so on.) and was not intended to be an interface to a traditional database. It can, however, be effective as a storage device if your data can be expressed as simple objects. This article offers an introduction to the API, explains how objects are stored, demonstrates the process in action, and provides a code library to do the work."
That's effectively what he does. Uses the serialization api to allow storing arbitrary objects with the Preferences api...
Quick summary: The Preferences API in Java 1.4 doesn't support storing an Object value, but we can hack around that by serializing the Object into a byte array, and storing limited length chunks as prefs.
This is NOT a replacement for an object db, just a way to make storing your prefs more flexible.
Personally, I don't think it's worth the trouble. Preferences are already hierarchichal (so there's no real gain in data organization). In most cases this will save only a few lines of code.
And in exchange for those lines of code, we do lose some things: the complexity added by putting another layer on top of the prefs API, for one, and the fact that debugging would suck (instead of readable data in your preferences, you now have chunks of serialized object).
So.. go for it if it would make your life much easier to store prefs as objects. Everyone else... read the article ANYWAY -- because it shows nicely how simple it is to use the Preferences API -- but you probably needn't bother to download the source.
Caveat -- I don't maintain any large Java apps with lots of preferences... but it seems to me that most prefs won't come automatically as objects.
There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.