org.rhq.core.util
Class PropertiesFileUpdate

java.lang.Object
  extended by org.rhq.core.util.PropertiesFileUpdate

public class PropertiesFileUpdate
extends Object

This utility helps update one or more properties in a .properties file without losing the ordering of existing properties or comment lines. You can update changes to existing properties or add new properties. Currently, there is no way to remove properties from a properties file (but you can set their values to an empty string).

Note that this utility only works on simple properties files where each name=value pair exists on single lines (i.e. they do not span multiple lines). But it can handle #-prefixed lines (i.e. comments are preserved).

This utility takes care to read and write using the ISO-8859-1 character set since that is what Properties uses to load and store properties, too.

Author:
John Mazzitelli

Constructor Summary
PropertiesFileUpdate(String location)
          Constructor given the full path to the .properties file.
 
Method Summary
 Properties loadExistingProperties()
          Loads and returns the properties that exist currently in the properties file.
 void update(Properties newProps)
          Updates the existing properties file with the new properties.
 void update(String key, String value)
          Updates the properties file so it will contain the key with the value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesFileUpdate

public PropertiesFileUpdate(String location)
Constructor given the full path to the .properties file.

Parameters:
location - location of the file
Method Detail

update

public void update(String key,
                   String value)
            throws IOException
Updates the properties file so it will contain the key with the value. If value is null, an empty string will be used in the properties file. If the property does not yet exist in the properties file, it will be appended to the end of the file.

Parameters:
key - the property name whose value is to be updated
value - the new property value
Throws:
IOException

update

public void update(Properties newProps)
            throws IOException
Updates the existing properties file with the new properties. If a property is in newProps that already exists in the properties file, the existing property is updated in place. Any new properties found in newProps that does not yet exist in the properties file will be added. Currently existing properties in the properties file that are not found in newProps will remain as-is.

Parameters:
newProps - properties that are added or updated in the file
Throws:
IOException

loadExistingProperties

public Properties loadExistingProperties()
                                  throws IOException
Loads and returns the properties that exist currently in the properties file.

Returns:
properties that exist in the properties file
Throws:
IOException


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.