更新属性文件中的属性值而不删除其他值[重复]

2024-01-12

内容First.properties:

name=elango
country=india
phone=12345

我要变化country from india to america。这是我的代码:

import java.io.*;
public class UpdateProperty 
{
    public static void main(String args[]) throws Exception 
    {   
        FileOutputStream out = new FileOutputStream("First.properties");
        FileInputStream in = new FileInputStream("First.properties");
        Properties props = new Properties();
        props.load(in);
        in.close();
        props.setProperty("country", "america");
        props.store(out, null);
        out.close();
    } 
}

输出内容为First.properties:

country=america

其他属性被删除。我想更新特定属性值,而不删除其他属性。


关闭输入流后,打开输出流并存储属性。

try (FileInputStream in = new FileInputStream("First.properties")) {
    Properties props = new Properties();
    props.load(in);
}
        
try (FileOutputStream out = new FileOutputStream("First.properties"))
    props.setProperty("country", "america");
    props.store(out, null);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新属性文件中的属性值而不删除其他值[重复] 的相关文章

随机推荐