Multiple Value initialization and retrieval by put and get method

In this example you are going to find out the flow of using put and get method of the preferences. After reading, coding, and running you are able to use the get and put method.


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Multiple Value initialization and retrieval by put and get method

     

In this example you are going to find out the flow of using put and get method of the preferences. After reading, coding, and running you are able to use the get and put method. Here we are using some extra package method like Arrays, Iterator here you can see that how firstly in program we putting the value of the preferences like int type ,Boolean type, and simple key value after that we are just using the get method we access all the stored value of the preferences. As we recently completed all of the put and get type, some of them are given here for knowing them more clearly.

Here is the code of the Program :

package roseindia;
import java.util.Arrays;
import java.util.Iterator;
import java.util.prefs.Preferences;

public class Basic {
  public static void main(String[] args) throws Exception {
  Basic basic = new Basic();
  }

  public Basic() throws Exception{
  Preferences prefs = Preferences.userNodeForPackage(this.getClass());
  prefs.put("city1""delhi");
  prefs.putInt("intValue"2);
  prefs.putBoolean("booleanValue"true);
  int usageCount = prefs.getInt("intValue", 0);
    usageCount++;
  prefs.putInt("UsageCount", usageCount);
  Iterator it = Arrays.asList(prefs.keys()).iterator();
  while (it.hasNext()) {
  String key = it.next().toString();
    System.out.println(key + ": " + prefs.get(key, null));
  }
  }
}

After thoroughly knowing the code you should save the file by "Basic.java" file name and when after compiling the code you run your program you will get the output given below .

Here is the output of the Program :

C:\roseindia>javac Basic.java
C:\roseindia>cd..
C:\>java roseindia.Basic
city1: delhi
intValue: 2
booleanValue: true
UsageCount: 3
C:\>_

In the above Program the City1 and Delhi is putting as the default value and the all of the rest value is putting as the integer , Boolean value and after using the put function we are also use the get function via which we are just take from the contains preferences value for the output.

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Multiple Value initialization and retrieval by put and get method

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Multiple Value initialization and retrieval by put and get method  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads