Wednesday, March 20, 2013
Today I am going to tell how you can save your application preferences, settings and configuration. You must have always wondered that how different softwares save the user data for that particular application. We can do this very easily in Java using the class java.util.prefs.Preferences .
Preferences class : This class allows applications to store and retrieve user and system preference and configuration data. This data is stored persistently in an implementation-dependent backing store. Typical implementations include flat files, OS-specific registries, directory servers and SQL databases. The user of this class needn't be concerned with details of the backing store. A node is created using node() method.
         There are two separate trees of preference nodes, one for user preferences (method used is userRoot()) and one for system preferences (method used is systemRoot()). Each user has a separate user preference tree, and all users in a given system share the same system preference tree. The precise description of "user" and "system" will vary from implementation to implementation. Typical information stored in the user preference tree might include font choice, color choice, or preferred window location and size for a particular application. Typical information stored in the system preference tree might include installation configuration data for an application. The put() method has two arguments of key-value pair. There are different put() methods for different data types. There is also a get() method for retreiving values of keys. The two parameters are key and default value. The default value is returned if key is not found. The keys and  nodes are removed using remove() and removeNode().
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------

import java.util.prefs.Preferences;

public class AppPreferences{
  public static void main (String[] args) throws Exception{
    Preferences p=Preferences.userRoot().node("myapp");
    System.out.println(p); //display current preference
    p.put("user","nirupam"); //adding a user key
    System.out.println(p.get("user","Hello World")); //shows default value if 
key not found
    p.remove("user"); //removing key
    p.removeNode();  //removing node
    }
}
--------------------------------------------------------------------------------------------------------------------------
Oitput
--------------------------------------------------------------------------------------------------------------------------
Screenshot of the windows registry
Windows Registry
To see the above outout in your registry you must comment the last two lines of code as they removes the node. So keep this in mind.

Output on Console

User Preference Node: /myapp
nirupam

--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.