Java Properties

Posted: 5 December 2007 in Uncategorized
Tags: , , , , , ,

I discovered the java.util.Properties class a couple weeks ago in the ginormous Java API docs. If you’ve ever created a software project where you have a lot of different settings that change frequently, this is the class for you. In my research, I implement all these different algorithms for various things, find out they don’t work, implement something else, rinse, repeat. Being able to look back at my results from two months ago and then loading the exact same configuration and running the experiment all over again is a must. Enter the Properties class.

So this is new in version Java SE 6 (according to the javadocs at least). This has been around since JDK 1.0 (update, see comments).  I had originally written my own program to do this. That was nice, but time-consuming and annoying. This class is more thorough, robust, and flexible. Basically you just create a text file (assuming you want to load from file) with properties in the following format:

Property = Value

The javadocs describe a couple of different viable formats, but I don’t use them. This style is cleaner and more intuitive than the others in my opinion. To load your properties (the class is just an extension of a HashTable) after creating your Properties object prop with the default constructor, just throw the following in a try-catch block:

prop.load(new FileInputStream("path-to-your-file"));

You then access properties with the appropriately named getProperty and setProperty methods (which work the same as get and put for Maps. I use a singleton class to load my properties. This way, the properties are universally accessible to all my classes without having to keep loading the file again and again.

Enjoy!

About these ads
Comments
  1. jhumphries says:

    The Properties class has actually been around since Java’s beginning (1.0). In fact, Java built their standard technique for internationalization around it – see java.util.ResourceBundle (which has been around since 1.1).

    I use this stuff all the time – it’s awesome. I’ve even built a MightyProperties class that supports contexts (so if I set my context to “something.cool”, then a query for the property “xyz” will search for a property named “something.cool.xyz”), inheritance (sort of anyways – in the above example I could exclude “something.cool.xyz” from the properties file and just specify “something.xyz” and then both contexts “something.cool” and “something.stupid” would use its value when querying for the property named “xyz” – a property in file named simply “xyz” would be a default used by all contexts if a more specific property did not exist), interpolation (so a property value could be defined as “${abc} boogers” and the value of the property named “abc” would be queried and substituted), and formulas (using another cool Java tool I mostly wrote [loosely based on an old component of the same name] – so a property value could be defined as “${=3*4} boogers” and result in a value of “12 boogers”).

    Another frequent use of properties files that I’ve encountered is for configuring dynamic systems – everything can be defined in terms of interfaces, and then the actual logic can dynamically look-up implementing class names from the properties and instantiate them at runtime via reflection.

    BTW, another cool API that is somewhat similar and much newer (1.4) is the Preferences API (java.util.prefs). It supports a hierarchy of preferences, similar to the Windows Registry. In fact, the default implementation on Windows uses the Windows Registry to store them – although there are also implementations that can use XML data files to load and store the preferences…

  2. Jason Adams says:

    Ah yeah I see the class was since jdk 1.0 now, I must have been looking at the load(Reader) method and didn’t think to check the class. Preferences package looks pretty useful too. More than I need for my current application, but a handy thing to know about for the future.

  3. Daniel says:

    Take a look at Commons Configuration: http://commons.apache.org/configuration/

    Its a generic configuration interface which enables a Java application to read configuration data from a variety of sources, such as:
    * Properties files
    * XML documents
    * Windows INI files
    * Property list files (plist)
    * JNDI
    * JDBC Datasource
    * System properties

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s