我正在尝试删除 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/