无法在Spring中写入属性文件

时间:2019-02-08 13:30:57

标签: java spring properties properties-file

我有下面的spring类,它尝试在属性文件中写入新的属性值:

@Service
public class Operations {

    public void changeProperties() {
        Properties prop = new Properties();
        OutputStream output = null;

        try {
            output = new FileOutputStream("rest-endpoint.properties");
            prop.setProperty("meta.db.device.url", "localhost");
            prop.store(output, null);
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
}

我也尝试了以下代码:

try {
    Properties props = new Properties();
    props.setProperty("Prop1", "toto");
    File f = new File("rest-endpoint.properties");
    OutputStream out = new FileOutputStream( f );
    DefaultPropertiesPersister p = new DefaultPropertiesPersister();
    p.store(props, out, "Header Comment");
} catch (Exception e ) {
    e.printStackTrace();
}

我的rest-endpoint.properties文件是:

meta.db.device.url=http://edgex-core-metadata:48081/api/v1/device
meta.db.devicemanager.url=http://edgex-core-metadata:48081/api/v1/devicemanager
meta.db.ping.url=http://edgex-core-metadata:48081/api/v1/ping

但是,当我执行代码时,属性文件中没有任何变化。任何想法可能出什么问题吗?

0 个答案:

没有答案