Even a chimp can write code

Wednesday, September 22, 2004

The latest gossip on java.util.Properties

The ubiquity of the Properties class is testament to its utility. It is a rare application that does not use an instance. I notice from the J2SE 5 javadocs that the java.util.Properties now supports an XML file -based backing store, leading to the addition of new methods:

public void storeToXML(OutputStream os, String comment)
throws IOException;

public void storeToXML(OutputStream os, String comment, String encoding)
throws IOException;

public void loadFromXML(InputStream in)
throws IOException,

that work off of an XML file with the schema:

<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >

which in turn looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<comment>some comment</comment>

<entry key="key1">value1</entry>
<entry key="key2">value2</entry>
<entry key="key3">value3</entry>


Now this is interesting. Purists -- in this case I fall into that category -- would argue that the simplicity of the key=value properties structure is the source of its strength and popularity. I do not see too much value in the XML form. The Java Preferences API hasn't gained much acceptance and the use of a totally different XML schema in the Properties class doesn't help that cause any.

And when are these deprecated methods going to be removed from the core Java source anyway? You know how we've always been instructed against catching exceptions but doing nothing about it (i.e. an empty catch clause)? Well the save method in the Properties class does exactly that. Take a look:

public synchronized void save(OutputStream out,
String header) {
try {
store(out, header);
} catch (IOException e) {

To Sun's credit, this method has been deprecated for a while, with developers advised to use the more proper store method. There're a number of deprecated methods in Java and we still have no idea when these will be dealt with (see Bug 5093875). Oh, well that one's for another day.

Email this | Bookmark this


  • A few reasons for the new XML format are multi-line keys and values as well as multi-line keys. The standard properties file format can deal with some of this, but the XML format makes it much easier to deal with those two issues. However, XML introduces other issues that need to be handled correctly. I filed a bug on form feeds in the new properties XML format. Blogged here, http://www.blojsom.com/blog/java/2004/09/15/java-50-bug-5102290.html. I think the XML format is a step in the right direction. It's at least a choice for handling certain scenarios in your application development.

    By Anonymous Anonymous, at September 22, 2004 at 7:29 PM  

  • Also being able to store text in a native language using UTF-8 instead of having to go through the step of native2ascii on properties files would be a nice benefit. I wonder if this extends to ResourceBundles? Will we be able to use messages_en_NZ.xml instead of messages_en_NZ.properties?

    By Anonymous Anonymous, at September 23, 2004 at 3:42 PM  

  • ResourceBundle or even PropertyResourceBundle strangely does not seem to support XML message files.

    By Blogger Ashish, at September 24, 2004 at 4:21 AM  

  • Well, .NET has had name/value properties in xml with it's app.config file format.

    I'm good with just the original Java simple text file of name=value properties, though.

    If I want to use xml for configuration info, then I want full xml with hierarchical stuctured elements and then use XPath query language to access things. Jazzing properties up to reside encapsulated in xml does nothing for me.

    By Blogger rogerv, at September 30, 2004 at 9:38 AM  

  • What wrecks my head with .properties files is that it does not allow s p a c e s in the key names. And why not?? Surely the '=' is a, nay _the_, delimiter between Key and Value?

    In frustration, and due to the proliferation of .properties files which appear in the sourcebase here, I've subclassed HashMap to include a
    void read(InputStream)
    method. And yes, it allows whitepace in the keys.

    You do it too.

    By Blogger Eanna, at October 19, 2004 at 7:26 AM  

  • Why can't you use spaces in a propeties file. Of course you can. It is basics to file systems. It has nothing to do with Java.
    Think a little bit before wrecking your head.

    By Anonymous Anonymous, at September 26, 2006 at 5:59 PM  

  • Joy in warcraft leveling living comes wow lvl from having wow lvl fine emotions,wow power level trusting them,power leveling giving them power leveling the freedom of wrath of the lich king power leveling a bird in the open.wlk power leveling Joy in living can age of conan gold never be assumed as a pose,or put on from guildwars gold the outside as a mask. People who have this joy don not need maple story mesos to talk about it; they radiate it. wow gold They just live out their joy and let wow power leveling it splash its sunlight and glow into other lives as naturally as bird sings.

    By Anonymous Anonymous, at February 3, 2009 at 5:03 PM  

Post a Comment | Home | Inference: my personal blog