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().
--------------------------------------------------------------------------------------------------------------------------
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
}
}
--------------------------------------------------------------------------------------------------------------------------
Screenshot of the 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 the source from Mediafire
DOWNLOAD the source from 4shared
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 |
Output on Console
User Preference Node: /myapp
nirupam
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:Utils
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment