Reputation: 235
I have a class that manages all the settings with methods:
public class SettingsManager {
public String settingsFileName = Start.programName + ".settings";
public SettingsManager(String settingsFileName) {
this.settingsFileName = settingsFileName;
}
public void saveIntoNewSettings(String nameOfSetting, String settingValue) {
Properties programProperties = new Properties();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(this.settingsFileName);
programProperties.setProperty(nameOfSetting, settingValue);
programProperties.store(outputStream, null);
outputStream.close();
}
catch (Exception errorException) {
Common.errorEncountered(errorException, "An error occured while trying to save a setting into the file " + this.settingsFileName + ".");
}
}
public void saveSetting(String nameOfSetting, String settingValue) {
try {
InputStream inputStream = new FileInputStream(this.settingsFileName);
OutputStream outputStream = new FileOutputStream(this.settingsFileName);
Properties programProperties = new Properties();
programProperties.load(inputStream);
inputStream.close();
programProperties.setProperty(nameOfSetting, settingValue);
programProperties.store(outputStream, null);
outputStream.close();
}
catch (Exception errorException) {
Common.errorEncountered(errorException, "An error occured while trying to load a setting from the file " + this.settingsFileName + ".");
}
}
public String loadSetting(String nameOfSetting) {
Properties programProperties = new Properties();
InputStream inputStream = null;
try {
inputStream = new FileInputStream(this.settingsFileName);
programProperties.load(inputStream);
inputStream.close();
return programProperties.getProperty(nameOfSetting);
}
catch (Exception errorException) {
Common.errorEncountered(errorException, "An error occured while trying to load a setting from the file " + this.settingsFileName + ".");
}
return "";
}
public boolean settingsFileExists() {
File settingsFile = new File(settingsFileName);
if (settingsFile.exists() && !settingsFile.isDirectory()) {
return true;
}
else {
return false;
}
}
}
After creating an instance of this class named "mainSettings", I run these two lines of code in my main class:
mainSettings.saveSetting("hello1", "world1");
mainSettings.saveSetting("hello2", "world2");
In my settings file, all I see is this:
hello2=world2
For some reason every time I call the saveSetting method it gets rid of older values in the settings file. I thought that those values should be saved when I called "programProperties.load(inputStream);
".
Upvotes: 0
Views: 322
Reputation: 5463
When you create the OutputStream the file is being truncated to 0 bytes. Infect you do not have any properties read. Try this:
public void saveSetting(String nameOfSetting, String settingValue) {
try {
InputStream inputStream = new FileInputStream(this.settingsFileName);
Properties programProperties = new Properties();
programProperties.load(inputStream);
inputStream.close();
OutputStream outputStream = new FileOutputStream(this.settingsFileName);
programProperties.setProperty(nameOfSetting, settingValue);
programProperties.store(outputStream, null);
outputStream.close();
}
catch (Exception errorException) {
Common.errorEncountered(errorException, "An error occured while trying to load a setting from the file " + this.settingsFileName + ".");
}
}
Should solve your issue.
Upvotes: 1