java 从属性文件中删除键和值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4225794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete key and value from a property file?
提问by harishtps
I want to delete key and value which is stored in a property file. How can i do that????
我想删除存储在属性文件中的键和值。我怎样才能做到这一点????
回答by BalusC
First load()
it using the java.util.Properties
API.
首先load()
它使用java.util.Properties
API。
Properties properties = new Properties();
properties.load(reader);
Then you can use the remove()
method.
然后就可以使用该remove()
方法了。
properties.remove(key);
And finally store()
it to the file.
最后store()
它到文件。
properties.store(writer, null);
See also:
也可以看看:
回答by Kasinaat 007
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
Here you can see that I could remove a key value pair using remove() method.
However the remove() method is a part of the HashTable object.
It is also available in properties because properties is a subclass of HashTable
在这里你可以看到我可以使用 remove() 方法删除一个键值对。
但是 remove() 方法是 HashTable 对象的一部分。
它也可以在属性中使用,因为属性是 HashTable 的子类