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
[email protected]

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

Thank you.

2 Answers 2

2

You are declaring a new Properties every time and add just windowstate to it. How could it know about anything else?

You have to:

  1. load the properties from the file

  2. add and/or modify settings

  3. save the properties to the same file

Sign up to request clarification or add additional context in comments.

Comments

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();

1 Comment

Welcome to SO :-) Just a couple of comments: a) please learn how to format code (can be tempting in the beginning, as I remember from my first days here :-) b) adding a bit of explanation to a code snippet helps future reader to understand the solution better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.