gpt4 book ai didi

java - 删除 HashMap 条目并将其写入文件

转载 作者:行者123 更新时间:2023-11-30 07:57:27 24 4
gpt4 key购买 nike

我正在尝试删除一个 HashMap 条目并将其重写到现有文件中,但我似乎无法让 HashMap 写入已删除条目的文件,尽管它会在本地删除该条目。

if(map.containsKey(lastName)){

map.remove(lastName);
//Stores all entries from the HashMap in the properties object
properties.putAll(map);
//Stores the HashMap in E:\Desktop\Snackdata.properties
properties.store(new FileOutputStream("E:\\Desktop\\SnackData.properties"), null);
}

else {

System.out.println("There is no entry under that name.");
}

最佳答案

properties.putAll(map);

不会从属性对象中删除任何内容。

你还需要调用

properties.remove(lastName);

关于java - 删除 HashMap 条目并将其写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com