根据简单的ini4j教程,我编写了一个类来读取和写入JDBC连接。下面是我点击对话框按钮时所做的:
public void actionPerformed(ActionEvent ae){
JButton b = (JButton)ae.getSource();
if (b == save || b == load) {
try {
Ini ini;
String section = name.getText();
if (b == load) {
System.out.println("Loading " + section);
ini = new Ini(new File(cfgname));
driver.setText(ini.get(section, "Driver"));
url.setText(ini.get(section, "URL"));
username.setText(ini.get(section, "User"));
password.setText(ini.get(section, "Password"));
} else {
System.out.println("Saving " + section);
ini = new Ini(new File(cfgname));
ini.put(section, "Driver", driver.getText());
ini.put(section, "URL", url.getText());
ini.put(section, "User", username.getText());
ini.put(section, "Password", password.getPassword());
ini.store();
} // endif b
} catch (FileNotFoundException fe) {
System.out.println(cfgname + ": not found " + fe);
setVisible(false);
} catch (IOException ioe) {
System.out.println(ioe);
setVisible(false);
} // end try/catch
} else {
id = (ae.getSource() == ok);
setVisible(false);
} // endif b
}//动作执行结束
阅读效果很好,但当点击"保存"时,写作会产生以下影响:
新的部分和值写入内存(我可以重新加载它们)但文件没有更新,保持不变。
我错过了什么?
您忘记了以下代码:
try {
fontOption.store(new FileOutputStream("config/font.conf"));
} catch(IOException e) {
System.err.println("font: cannot load font.conf or default.conf");
}
…