简体   繁体   中英

How to write a data into the application.properties file in java

I am working on some automation stuff, in which I just want to write the values into the application.properties file as shown below:

project.ids = 01234, 56789, 14587,...

So here, my key( project.ids ) will be the same, I just want to append the ids to the previously stored values (shown above). Whenever my service will be called a new project id will be generated so I just want to append or store the value in the property file to the same key( project.ids ).

Could anyone help or suggest here how can I achieve this? Any suggestions around how to write the value to the application.properties file in Java?

I would suggest to use the Environment class of Spring. But it is only used to read properties and not write them.

what you could do is follow this way:

  1. Load the .properties file:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("application.properties");
prop.load(in);
  1. Get the value as it is of the property
String ids = prop.getProperty("project.ids");
  1. Set the new value
String ids = prop.setProperty("project.ids", ids + ", 12324...");

PS: I wouldn't play around with the .properties file much, it is mostly used for configurations. Try saving your ids in a DB, Spring can be very helpful for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM