我有CrunchifyGetPropertyValue类,可以从config.properties示例中接收参数,如下所示:
...
public int getParamAnswersValue() throws IOException{
try{
Properties prop = new Properties();
String propFileName = "config.properties";
inputStream = this.getClass().getResourceAsStream("/res/config.properties");
if(inputStream != null){
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
value = Integer.parseInt(prop.getProperty("paramAnswers"));
System.out.println(value);
} catch (Exception e){
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return value;
}
...
还有OptionsController.java,其中包含读取/更新config.properties示例的任务,如下所示:
...
CrunchifyGetPropertyValue properties = new CrunchifyGetPropertyValue();
@Override
public void initialize(URL location, ResourceBundle resources) {
try{
slider.setValue(properties.getParamAnswersValue());
field.setText(Integer.toString(properties.getParamAnswersValue()));
field.textProperty().bind(slider.valueProperty().asString("%.0f"));
...
} catch (IOException ex) {
Logger.getLogger(OptionsController.class.getName()).log(Level.SEVERE, null, ex);}
问题是,该选项类可以成功将更新保存到config.file,但是在保存之后,当我再次在同一运行中打开此类时,slider.value和field.text确实显示了第一个初始化值在屏幕上,当config.properties在文件中更改自身时,我们可以观察到。示例:
>Run App
>>Open optionPanel //run OptionsController.java
>>>//OptionsController loaded our config.properties data (2:3:4)
>>>We change values from 2:3:4 to 3:3:3
>>>click saveButton
//now we can see by openning config.properties that actually there are 3:3:3 values
>>>optionPanel window is closing
>>Open optionPanel //we open window again in same run
>>>//OptionsController loaded values 2:3:4 instead of 3:3:3
>Close app
>Run app
>>Open optionPanel //run OptionsController.java
>>>//OptionsController loaded our config.properties data (3:3:3)
....
为什么在同一运行中它没有变化? 尝试了几件事但无济于事,有什么主意为什么不从同一应用程序运行时从.properties中读取数据?