ini4j store not working
Following the simple ini4j tutorial I wrote a class to read and write JDBC connections. Here is what I do when clicking on the dialog buttons:
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 = 开发者_JAVA技巧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
} // end of actionPerformed
Reading works well but writing when hitting "save" does the following:
New section and values are written in memory (I can reload them) But the File is not updated and remains the same.
What am I missing?
You forgot the following code:
try {
fontOption.store(new FileOutputStream("config/font.conf"));
} catch(IOException e) {
System.err.println("font: cannot load font.conf or default.conf");
}
... that is stored in the file changes.
精彩评论