Hi,
This class should take care of ur problems..its a pretty long one though.. but takes care of saving the properties object and everything..
Hope this helps,
Reddy316.
import java.util.Enumeration;
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Allows resource bundling
*/
public class Settings
{
//The single instance
private static Settings instance;
//The filename of the property file
private String strFileName;
private Properties oProperties;
/**
* Constructs a Settings
* @exception IOException when the file may not be present
* @param String the file name
*/
private Settings(String strFileName) throws IOException
{
oProperties = new Properties();
setFilename(strFileName);
}
/**
* method to get the single instance of this class
* @return the single instance of this class based on the file name specified.
* @param the name of the config file
* @exception IllegalStateException thrown when the single instance has not
* been created via a call to setInstance().
*/
public static Settings getInstance(String strFileName)
{
String strPropfile = "";
if (instance == null)
{
strPropfile = strFileName;
try
{
instance = new Settings(strPropfile);
}
catch (Exception e)
{
throw new IllegalStateException("Failed to load property settings."

;
}
}
return instance;
}
/**
* Sets the property file used by the single instance.
* @param String the config file name to be picked up
* @exception IllegalArgumentException thrown when the filename is null.
*/
public void setFilename(String strFileName) throws IOException
{
if (strFileName == null)
{
throw new IllegalArgumentException("filename cannot be null"

;
}
else if (!strFileName.equals(this.strFileName))
{
if (this.strFileName != null)
{
save(); //save current property file before replacing
}
this.strFileName = strFileName;
load(strFileName);
}
}
/**
* method to get the current config file associated with the instance
* @return String the filename of the property file used by the single instance.
*/
public String getFilename()
{
return strFileName;
}
/**
* method to load the current config file associated with the instance
* @param the filename of the property file used by the single instance.
*/
public void load(String strFileName) throws IOException
{
oProperties.load(new FileInputStream(strFileName));
}
/**
* method to save the new property values back to the property file.
*/
public void save()
{
if (strFileName == null)
return;
try
{
FileOutputStream fos = new FileOutputStream(strFileName);
oProperties.store(fos,strFileName);
}
catch (IOException e)
{
System.out.println("Failed to save properties"

;
}
}
/**
* method to get the properties file associated with the instance of this class
* @return the Properties object used by Settings
*/
public Properties getProperties()
{
return oProperties;
}
/**
* method to check if the key exists in the property file
* @return boolean true if key is found, false if the key is not present
*/
public boolean containsKey(String key)
{
return oProperties.containsKey(key);
}
/**
* method to return the value present in the property file for the given key
* @param String the key for which the value is to be found
* @return String the property value given its key,returns null if the key is not found.
*/
public String getProperty(String key)
{
return oProperties.getProperty(key);
}
/**
* method to return the value present in the property file for the given key
* @return String the property value given its key, returns defaultValue if the key
* is not found.
*/
public String getProperty(String key, String defaultValue)
{
return oProperties.getProperty(key, defaultValue);
}
/**
* method to get the keys in the property file as enumeration
* @return Enumeration an enumeration of the property keys/names.
*/
public Enumeration propertyNames()
{
return oProperties.propertyNames();
}
/**
* method to set the key value pair in the property file
* @param String the key
* @param String the value to be associated with the key
*/
public void setProperty(String key, String value)
{
oProperties.setProperty(key, value);
}
}