How to write values in a properties file through java code

前端 未结 4 1910
庸人自扰
庸人自扰 2020-11-30 09:30

I have an issue.

I have a properties file. I want to store some values in that file and will implement in the code whenever it is required. Is there any way to do t

4条回答
  •  醉梦人生
    2020-11-30 10:23

    Your problem is not clear since Writing/reading from properties files is something already available in java.

    To write to properties file you can use the Properties class as you mentioned :

    Properties properties = new Properties();
    try(OutputStream outputStream = new FileOutputStream(PROPERTIES_FILE_PATH)){  
        properties.setProperty("prop1", "Value1");
        properties.setProperty("prop2", "Value2");
        properties.store(outputStream, null);
    } catch (IOException e) {
        e.printStackTrace();
    } 
    

    Source and more examples here

提交回复
热议问题