我想删除存储在属性文件中的键和值。我怎样才能做到这一点????
问问题
23841 次
2 回答
41
首先load()
它使用java.util.Properties
API。
Properties properties = new Properties();
properties.load(reader);
然后就可以使用该remove()
方法了。
properties.remove(key);
最后store()
是文件。
properties.store(writer, null);
也可以看看:
于 2010-11-19T14:17:15.150 回答
1
public class SolutionHash {
public static void main(String[] args) throws FileNotFoundException,IOException {
FileReader reader = new FileReader("student.properties");
Properties properties = new Properties();
properties.load(reader);
// System.out.println(properties);
Enumeration e = properties.propertyNames();
while(e.hasMoreElements()){
String key = (String)e.nextElement();
if(key.equals("dept"))
properties.remove(key);
else
System.out.println(key+"="+properties.getProperty(key));
}
// System.out.println(properties);
}
}
OUTPUT:
name=kasinaat
class=b
在这里你可以看到我可以使用 remove() 方法删除一个键值对。
然而 remove() 方法是 HashTable 对象的一部分。
它也可以在属性中使用,因为属性是 HashTable 的子类
于 2018-05-21T06:04:39.603 回答