Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unintentionally overwritting properties 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
0
0
CH
Hi,

I store my properties to a file after i populated them with strings using the setProperty(String key, String value) method. However, it keeps overwriting the properties that i have not changed during this action. So I first loaded the properties with an input stream of the file. Didn't help neither. How do I need to store correctly without loosing the other existing entries in the properties file?

Code:
Properties prop = new Properties();
String propFile = "User.properties";
FileOutputStream out = new FileOutputStream(propFile);
Date d = new Date();
try {
    prop.load(new FileInputStream(propFile));
    prop.setProperty("Room", "23");
    prop.store(out, "Saved on " + d.toString());
} catch (IOException ex) {
    Logger.getLogger(JPanelUserSettings.class.getName()).log(Level.SEVERE, null, ex);
}
 
Okay, here's the solution. One needs to load the
Code:
prop.load(new FileInputStream(propFile));
before the
Code:
FileOutputStream out = new FileOutputStream(propFile);
 
Don't forget to close the file after finishing and in the catch block ...

Cheers,
Dian
 
From looking at the API there's no close method. What method do you refer to?

thanks for the comment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top