I want to delete key and value which is stored in a property file. How can i do that????
Asked
Active
Viewed 2.7k times
2 Answers
42
First load()
it using the java.util.Properties
API.
Properties properties = new Properties();
properties.load(reader);
Then you can use the remove()
method.
properties.remove(key);
And finally store()
it to the file.
properties.store(writer, null);
See also:

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
It is used to remove the key only if it is stored in a hashtable... I tried its not working........:( – harishtps Nov 19 '10 at 14:20
-
Did you close the `Writer` afterwards the usual Java IO way? – BalusC Nov 19 '10 at 14:22
-
Is their a way to do this in ant? – Jono Jan 23 '13 at 16:01
-
And how to do it if I use not the Properties but propertiesConfiguration to load file? – Nikitin Mikhail Jul 31 '13 at 10:12
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
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

Kasinaat 007
- 124
- 14