有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Spring中写入属性文件

我有以下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) 个答案