• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Preference API

 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the overview
and here's the API
 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi, Properties files may serve your purpose.
 
Ravi kapa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is EasyConfig?Can it also useful for storing preferences...can anyone give me information about it please??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic