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