Java 读取和存储属性文件

Java 读取和存储属性文件,java,properties,Java,Properties,我试图通过以下代码更新属性文件中的值 导入java.io.*; 导入java.util.Properties public class Sample { public static void main(String a[]) throws IOException { InputStream is = Sample.class.getClassLoader().getResourceAsStream("myfile.properties"); Propert

我试图通过以下代码更新属性文件中的值

导入java.io.*; 导入java.util.Properties

public class Sample {
    public static void main(String a[]) throws IOException {
        InputStream is = Sample.class.getClassLoader().getResourceAsStream("myfile.properties");
        Properties p = new Properties();
        p.load(is);

        p.setProperty("myProperty", "updated");

        OutputStream os = new FileOutputStream("myfile.properties");
        p.store(os, "update");
        os.close();
        System.out.print(p.getProperty("myProperty"));
    }
}
输出:更新


但是这些值似乎没有得到更新。事实上,即使属性或文件本身不存在,我也不会收到任何错误。

此属性文件是否构建在一个JAR中?不,我已将所有属性文件构建在一个单独的目录中。此属性文件是否构建在一个JAR中?不,我已将所有属性文件构建在一个单独的目录中。我如上所述更改了代码。现在的问题是,属性文件得到更新,但在程序结束之前不会刷新。因此,我以后无法在同一个程序中检索更新后的值。现在的问题是,属性文件得到更新,但在程序结束之前不会刷新。因此,我以后无法在同一个程序中检索更新的值。
// Read properties file.
Properties prop = new Properties();

try {
    prop.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}

// Write properties file.
try {
    prop.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}