更新属性文件中的属性值而不删除其他值 [英] Updating property value in properties file without deleting other values
本文介绍了更新属性文件中的属性值而不删除其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的内容First.properties
:
name=elango
country=india
phone=12345
我想要更改 country
从 india
到美国
。这是我的代码:
I want change country
from india
to america
. This is my code:
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();
}
}
输出内容首先。属性
:
country=america
其他属性将被删除。我想更新特定的属性值,而不删除其他属性。
The other properties are deleted. I want update a particular property value, without deleting the other properties.
推荐答案
关闭后打开输出流并存储属性输入流。
Open the output stream and store properties after you have closed the input stream.
FileInputStream in = new FileInputStream("First.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("First.properties");
props.setProperty("country", "america");
props.store(out, null);
out.close();
这篇关于更新属性文件中的属性值而不删除其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文