Monday, January 4, 2010

Java Properties Class Vs Preferences Class

Whenever we needed to store user specific configuration, we could use the Properties class. One issue using Properties class is where to store those files, here simple file system or network files would be used to store those informations, but what happens if user does not have write permission on those file systems.

Here the Preferences API comes into the existence, this API is available since Java 1.4, this API resolves these problems. The purpose of the API is to allow for the easy storage of user preferences without the developer having to worry about where this information will be stored. The API determines the appropriate place.
For example, when running an application in Windows, the preferences will be stored in the Windows registry.

This API is found in java.util.prefs package. All preferences are stored like tree-structure with the data itself stored in nodes of the tree.

This API provides two types of Preferences data:
   1. User Preferences
   2. System Preferences

1. User Preferences are only visible to the user, for which it is created.
2. System Preferences are shared among all the users.

The Preferences class has several methods used to get and put primitive data in the Preferences data store. We can use only the following types of data:
   * String
   * boolean
   * double
   * float
   * int
   * long
   * byte array

Example:


Output:


If we use Preferences API in Windows platform, then Preference data will be stored in Windows Registry.

No comments:

Post a Comment