需求:有一个文本文件,数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其值为”100”
分析:
把文本文件中的数据加载到Properties集合中
判断这个集合中是否有"lisi"这个键
如果有直接修改其值为100
把集合中的数据再次存储到文本文件中
public class MyTest2 {
public static void main(String[] args) throws IOException {
//我有一个配置文件,数据是键=值形式,让你判断他有没有lisi这个键,如果有把他的值改成100
Properties properties = new Properties();
properties.load(new FileInputStream("user.properties"));
if (properties.containsKey("lisi")) {
properties.setProperty("lisi","王五");
properties.store(new FileWriter("user.properties"),null);
}else{
System.out.println("没有这个键");
}
}
}