• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Properties through a Property file

 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the following class from my main function in following way
PropertyLoader.Instance().loadProperties("Properties.txt");
PropertyLoader.Instance().addProperty("HELLO","DOCTOR");
but my Properties.txt file is not getting updated
with the new entries
What changes do I have to make in it to make it the way i desired

public class PropertyLoader extends java.util.Properties {
private static PropertyLoader instance;

private PropertyLoader(){}
public static PropertyLoader Instance(){
if(instance==null)
{
instance = new PropertyLoader();
}
return instance;
}
public int getIntProperty(String key){
int intValue=0;
if(propertyIsNull(key)==true)
{
Logger.Instance().setError(true);
Logger.Instance().logError("Property: "+key+" is not loaded and has no value");
}
else
{
Integer intObj = new Integer(this.getProperty(key));
intValue = intObj.intValue();
}
return intValue;
}
public double getDoubleProperty(String key){
double dblValue=0;
if(propertyIsNull(key)==true)
{
Logger.Instance().setError(true);
Logger.Instance().logError("Property: "+key+" is not loaded and has no value");
}
else
{
Double dblObj = new Double(this.getProperty(key));
dblValue = dblObj.doubleValue();
}
return dblValue;
}

public boolean propertyIsNull(String key){
if(this.getProperty(key).equals(null))
return false;
else
return true;
}
public void addProperty(String key,String value){
setProperty(key,value);
}
public void loadProperties(String fileName){
try {
String path = System.getProperty("java.home")+File.separator+"lib"+File.separator+fileName;
System.out.println(path);
File file = new File(path);
if(file.exists())
load(new BufferedInputStream(new FileInputStream(file)));
}
catch(Exception fex) {
Logger.Instance().setError(true);
Logger.Instance().logError(fex.toString());
}//catch
}
}
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void addProperty(String key,String value){
setProperty(key,value);
}

It looks like you are thinking that the setProperty() method called above will add the new property to the file, but it does not. It only adds the key/value to its internal hashtable so you can get to it via the getProperty() method. If you need to physically update your properties file, check out the store() method of Properties.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, it's greatly appreciated if you enclose code samples within UBB Code tags
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!