1

I have designed a Java application and I want to save its last window state and some other settings like date format. I currently save window state to "config.ini" file and it works fine for this code.

    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) {
        }
    }

But I want to save some other settings too. For that can I update individual parameters of this config.ini file? (Currently there is only "windowstate", in case there is something like date format, last used email address etc...)

My current file is like this

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

E.g. If config.ini file is like this

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

can I update only "lastmailaddress" without affecting others? and how? Currently my code is overwriting this file.

Thank you.

4

2 回答 2

2

Properties您每次都在声明一个新的并添加windowstate它。它怎么可能知道其他任何事情?

你必须:

  1. 从文件中加载属性

  2. 添加和/或修改设置

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

于 2013-07-07T17:48:34.867 回答
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();
于 2015-09-09T08:58:02.177 回答