1

I'm using the below code to update data in my existing config.properties file. Once it is written, the config.properties file's data are not in the same sequence as it was. How can I write/update the config file so that it only updates that specific key value and doesn't mess up the sequence of how it has been written?

Existing config.properties file

browser = chrome
invalid_username = abc123
invalid_password = abc123
invalid_email = testQA@test.com


Update_Contact_info = PA Address
address1= 123 broad st
address2= 990
city= Horsham
state= KY
zipCode= 19044
homePhone= 1111111111
workPhone= 1111111111
cellPhone= 2155551212
email= qa1.ramkumar@abc.com


Update_Contact_info = NY Address
nyAddress1= 101 broad st
nyAddress2= none
nyCity= queens
nyState= NY
nyZipCode= 11797
nyHomePhone= 2152375757
nyWorkPhone= 1111111111
nyCellPhone= 2155551212
nyEmail= qa.fry@abc.com

code to update config.properties data

public static void updateConfigPropertiesValue(String key, String value) {
        File configFile = new File(System.getProperty("user.dir") + "/src/main/java" + "/com/abc/config/config.properties");
        try {
            Properties props = new Properties();
            prop.setProperty(key, value);
            //prop.store(new FileOutputStream(System.getProperty("user.dir") + "/src/main/java" + "/com/abc/config/config.properties"), "");
            FileWriter writer = new FileWriter(configFile);
            prop.store(writer, "");
            writer.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            // I/O error
        }
    }

Updated config.properties file

Updated config.properties file

#
#Wed Feb 26 15:27:12 EST 2020
invalid_email=testQA@test.com
invalidState=TX
state=KY
nyCellPhone=2155551212
invalidEmail=DEVELOPMENT@abcCOM
invalidCity=Ambler
city=Horsham
invalidAddress1=1635 SHERMAN ST NE
invalidAddress2=99999
nyZipCode=11797
email=qa1.ramkumar@abc.com
cellPhone=2155551212
zipCode=19044
invalidZipCode=99999
nyHomePhone=2152375757
invalidWorkPhone=2i52376268
homePhone=1111111111
nyCity=WOODBURY
nyState=NY
nyAddress2=none
nyAddress1=101 broad st
invalid_password=abc123
nyWorkPhone=1111111111
browser=chrome
invalidCellPhone=6666666666
invalidHomePhone=9 90290000
invalid_username=abc123
address2=120
address1=123 broad st
nyEmail=qa.fry@abc.com
workPhone=1111111111
pcalkins
  • 1,188
  • 13
  • 20
Radi
  • 13
  • 4
  • Java's "Properties" is a hashtable more or less which doesn't really need sorting. Any particular reason order is important? – pcalkins Feb 26 '20 at 23:35
  • First read all the keys in insertion order in a collection, then update the collection with specific key/value, then re-write the data back to file in same insertion order. This should be the intended flow. – Kumar Ashutosh Feb 27 '20 at 04:15
  • @pcalkins, there are many clients information that I am maintaining in the config file. Time to time we need to update the config file to change username, pw, personal information. It is hard to locate things if it is not in sequence. Any help is appreciated. – Radi Feb 27 '20 at 15:30
  • Also, the client URL changes to **abc_url=https\://uat.payment.global.com/** which is not going to the destination. – Radi Feb 27 '20 at 15:49
  • You might want to use another format... but for .config files, you want a class that loads your config into "Properties" (hashtable) and has methods to set values, delete values... those methods take the key. Change the "Properties" object, then write it to the file. The properties object will mirror the file. Get/read methods can just read from the Properties object. It's designed to be used for global program options. (Things like saving the window size, last used directories, or global program data...) – pcalkins Feb 27 '20 at 19:14
  • So basically it's better to create a little app for updating the properties rather than doing it in notepad or something like that. You could also just use Notepad and a simple .csv format... then read it in as a sortable array. CSVReader will load those files into a List. (You can use new ArrayList(); to initialize) – pcalkins Feb 27 '20 at 23:20

0 Answers0