Java Preference API

Java preference API is used to store the data of the program into system. Before it, Properties class was used to store the system configuration. The properties class the not capable to hold some security data because it can not hold the data for the long time. But java preference is the facility which allows as storing the data like registry into Microsoft operating system. The stored data may be used later. Developers no need to worry about the storage that where the data will save. The Java run time plat--forms manage it according to operating system.

import java.util.prefs.*;
class Pref1
{
        public static void main(String ar[])
        {
            Preferences prefs = Preferences.userRoot(  ).node("java/notes");
            prefs.put("author""Hemraj");
            prefs.putInt("edition"1);
            String author = prefs.get("author""unknown");
            int edition = prefs.getInt("edition"-1);
         }
}
=====================================================


import java.util.prefs.*;
class Pref1
{
         public static void main(String ar[])
         {
            Preferences prefs = Preferences.userRoot(  ).node("java/notes");
            String author = prefs.get("author""unknown");
            int edition = prefs.getInt("edition"-1);
            System.out.println(author+" , "+edition);
         }
}

In this program the program Pref2 stores the value Hemraj and 1 with the corresponding keys author and edition. Next program get these value using the same keys by which stored prior. 

No comments:

Popular Posts