我有一个使用方法管理所有设置的类:
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;
}
}
}
在创建名为" mainSettings"的这个类的实例后,我在我的主类中运行这两行代码:
mainSettings.saveSetting("hello1", "world1");
mainSettings.saveSetting("hello2", "world2");
在我的设置文件中,我看到的只有:
hello2=world2
出于某种原因,每次调用saveSetting方法时,它都会删除设置文件中的旧值。当我打电话给#34; programProperties.load(inputStream);
"。
答案 0 :(得分:1)
创建OutputStream时,文件被截断为0字节。感染您没有阅读任何属性。试试这个:
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 + ".");
}
}
应解决您的问题。