簡體   English   中英

讀取和存儲屬性文件

[英]reading and storing properties file

我正在嘗試通過以下代碼更新屬性文件中的值

導入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"));
    }
}

輸出:已更新

但是這些值似乎沒有更新。 實際上,即使屬性或文件本身不存在,我也不會收到任何錯誤。

// 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) {
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM