복붙노트

[SPRING] 봄에 속성 파일 값 쓰기 / 업데이트

SPRING

봄에 속성 파일 값 쓰기 / 업데이트

내 봄 응용 프로그램을 사용하여 속성 파일에 값을 쓰거나 업데이트하려는 일부 요구 사항이 있습니다.

나는 그것을 봤지만 나는 봄을 사용하여 그것을하는 직접적인 방법을 발견하지 못했습니다.

아무도 그것을하는 방법을 알고 있거나 그것을 할 수있는 가장 좋은 방법이 있습니다.

미리 감사드립니다.

해결법

  1. ==============================

    1.다음과 같이 달성 할 수 있습니다.

    다음과 같이 달성 할 수 있습니다.

    public void saveParamChanges() {
       try {
         // create and set properties into properties object
         Properties props = new Properties();
         props.setProperty("Prop1", "toto");
         props.setProperty("Prop2", "test");
         props.setProperty("Prop3", "tata");
         // get or create the file
         File f = new File("app-properties.properties");
         OutputStream out = new FileOutputStream( f );
         // write into it
         DefaultPropertiesPersister p = new DefaultPropertiesPersister();
         p.store(props, out, "Header COmment");
       } catch (Exception e ) {
        e.printStackTrace();
       }
    }
    

    출처

    편집 : org.springframework.Util에서 defaultPropertiesPersiter로 업데이트

  2. from https://stackoverflow.com/questions/31334977/write-update-properties-file-value-in-spring by cc-by-sa and MIT license