wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java Preference API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Preference API" Watch "Java Preference API" New topic
Author

Java Preference API

Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
Hi All,I want to do some coding for my requirement using Java Preference API.I read some of the articals and done some samples.When i export the preferences to the xml file it is exporting and showing values in xml.My problem is when i delete the preferences in the xml manually then also when i compile the code it is again getting the deleted values in the xml.My doubt is where the preferences are storing,whether it is storing in xml only or by defalut any other storage........And anyone can give me some Java Preference API urls where i can find some information...thanq...

This is my code
------------------
import java.util.*;
import java.util.prefs.*;
import java.io.*;

public class PreferenceExample {



public void printInformation(Preferences p) throws BackingStoreException{

String s=p.get("url", "");
System.out.println(s+":This is the url value");

}

public void setSomeProperties(Preferences p) throws BackingStoreException {

p.put("url","ldap://indc6:389");
p.put("username","ravikum@in.nds.com");
p.put("password", "Reddy@2504");
}

public void exportToFile(Preferences p, String fileName)
throws BackingStoreException {
try {
FileOutputStream fos = new FileOutputStream(fileName);

p.exportSubtree(fos);
fos.close();
} catch (IOException ioe) {
System.out.println("IOException in exportToFile\n" + ioe);
ioe.printStackTrace();
}
}

public static void main(String args[]) {
PreferenceExample pe = new PreferenceExample();
Preferences prefsRoot = Preferences.userRoot();
Preferences myPrefs = prefsRoot.node("PreferenceExample");
try {
pe.setSomeProperties(myPrefs);
pe.printInformation(myPrefs);
pe.exportToFile(myPrefs, "pref.xml");
} catch (BackingStoreException bse) {
System.out.println("Problem with accessing the backing store\n"+ bse);
bse.printStackTrace();
}
}
}


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your preferences are stored somewhere in your computer. You don't need to know where, all you need to know is that they are stored somewhere.

Exporting the preferences to some other place does not change that. It just makes a copy. The preferences are still stored where they always were. And modifying the copy of the preferences naturally doesn't affect the original in any way.
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
Paul Clapham wrote:Your preferences are stored somewhere in your computer. You don't need to know where, all you need to know is that they are stored somewhere.

Exporting the preferences to some other place does not change that. It just makes a copy. The preferences are still stored where they always were. And modifying the copy of the preferences naturally doesn't affect the original in any way.


Thank you paul,can you tell me some source of information (like urls) where i can find some more informaton regerding Java preference API....
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1077
    
  10

Here's the overview
and here's the API


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
Hi All,i have one doubt regarding my project requirement that is "Is it possible to store the preference in a custom location or file( instead of by default storing in windows registry)with java preference API"so that when we change the file it should get reflect in the Application code(because changing or storing preferences in the windows registry is not a good idea every time)...and if possible give some urls where i can find some information regarding this scenario...thank you..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
Ravi, Properties files may serve your purpose.


Ping & DNS - my free Android networking tools app
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
Ulf Dittmer wrote:Ravi, Properties files may serve your purpose.
yeah ulf thanks for the response ,But the problem is the properties file mechanism is not better serve for Tree like hierarchical structure(like naming conflicts and too many property files) thats why my lead plan for preference api. And with that we have custom storing problem(like i mentioned earlier)...
Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
what is EasyConfig?Can it also useful for storing preferences...can anyone give me information about it please??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Preference API