在Java应用程序中保存和更新配置设置

时间:2013-07-07 17:28:43

标签: java ini

我设计了一个Java应用程序,我想保存其最后一个窗口状态和一些其他设置,如日期格式。我目前将窗口状态保存到" config.ini"文件,它适用于此代码。

    private void saveConfig() {
        try {
            Properties properties = new Properties();

            properties.setProperty("windowstate", String.valueOf(this.getExtendedState()));

            properties.store(new FileOutputStream("./data/config.ini"), null);

        } catch (Exception e) {
        }
    }

但我也想保存其他一些设置。为此可以更新此config.ini文件的各个参数? (目前只有" windowstate",如果有类似日期格式的内容,最后使用的电子邮件地址等...)

我当前的文件是这样的

#Sun Jul 07 22:19:35 IST 2013
windowstate=0

E.g。如果config.ini文件是这样的

#Sun Jul 07 22:19:35 IST 2013
windowstate=0
dateformat=yyyy-MM-dd
lastmailaddress=abcd@mail.com

我可以只更新" lastmailaddress"不影响他人?如何? 目前我的代码正在覆盖此文件。

谢谢。

2 个答案:

答案 0 :(得分:2)

您每次都宣布一个新的Properties,并仅向其添加windowstate。怎么会知道其他什么呢?

你必须:

  1. 从文件中加载属性

  2. 添加和/或修改设置

  3. 将属性保存到同一文件

答案 1 :(得分:0)

FileInputStream in = new FileInputStream("D:/raman/abnconfig.ini");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("D:/raman/abnconfig.ini");
props.setProperty("HSMLUNAPWD", "AUS");
props.store(out, null);
out.close();