Here's another newbie question?
Is java.util.Properties the canonical way of reading and writing
configuration files?
alex
Hi
It seems that the article I mentioned in an earlier post
http://www.javaworld.com/javaworld/jw-08-2001/jw-0831-preferences-p2.html
has some 'inconsistencies' with my current version of J2SE 1.4.2. The
article mentions getting a reference to a Preferences object by
passing an object reference but my API docs only show a
userNodeForPackage method that takes a Class as argument so I thought
I'd do a quick run through of getting and setting preferences and see
if anyone can point out problems with this approach.
First set up a properties node
package anon.pack.prefs;
import java.util.prefs.Preferences;
import java.io.FileOutputStream;
public class PropertiesTest {
private Preferences prefs;
public PropertiesTest() {
prefs = Preferences.userNodeForPackage(this.getClass());
String outfile = "C:/prefs/prefs.xml";
prefs.put("outfile", outfile);
try{
prefs.exportNode(new FileOutputStream(outfile));
}
catch(Exception ex){
}
}
public static void main(String[] args){
new PropertiesTest();
}
}
After running this once I have the following file in
C:/prefs/prefs.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM
'
http://java.sun.com/dtd/preferences.dtd'>
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="anon">
<map />
<node name="pack">
<map />
<node name="prefs">
<map>
<entry key="outfile" value="C:/prefs/prefs.xml" />
</map>
</node>
</node>
</node>
</root>
</preferences>
I can now modify my code to read the preferences file location thus
....
public PropertiesTest() {
prefs = Preferences.userNodeForPackage(this.getClass());
String prefsfile = prefs.get("outfile", "C:/temp/tempprefs.xml");
try{
prefs.importPreferences(new FileInputStream(prefsfile));
}
catch(Exception ex){
ex.printStackTrace();
}
}
So now I can change the location of my preference file at will which
is pretty cool I think.
Anyway say I now need to set a 'property' that returns the value of a
listenPort (crap example but it does the job). All I need to do is to
edit my preferences file and add in a new entry like this.
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="anon">
<map />
<node name="pack">
<map />
<node name="prefs">
<map>
<entry key="outfile" value="C:/prefs/prefs.xml" />
<!-- *********** new entry *********** -->
<entry key="listenPort" value="26358" />
</map>
</node>
</node>
</node>
</root>
</preferences>
Now I can access this preference/property in my code
....
public String getPref(String key){
return prefs.get(key, "someDefaultValue");
}
public static void main(String[] args){
System.out.println(new PropertiesTest().getPref("listenPort"));
}
The output is
26358
Which seems pretty cool to me, I couldn't give a monkeys where the
information is stored on the platform, as far as I'm concerned I have
a platform independent, location independent way of specifying
properties for my programs.
I'm not really sure where the author of the article above is coming
from when he uses objects to get preferences, perhaps this was an
earlier version (or a later one, I haven't looked yet). Whatever, much
of what he says seems to make sense (to me at least).
What, if anything, do people think is wrong with this approach ?
Rgds
Lyall