Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete key and value from a property file?

Tags:

I want to delete key and value which is stored in a property file. How can i do that????

like image 953
harishtps Avatar asked Nov 19 '10 14:11

harishtps


People also ask

How remove value from properties file in Java?

Properties properties = new Properties(); properties. load(reader); Then you can use the remove() method.

How do I clear properties file?

To delete a properties file: In the Project Explorer in Oracle Policy Modeling, right-click the properties file and select Delete.

How do I get the key of a properties file?

HashTable Class, so has a keys() method. you can use this to retrieve a list of all the keys used in the file. This iterates through all the properties as key, value pairs until the pair with the correct value is found.


2 Answers

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:

  • Properties tutorial
like image 50
BalusC Avatar answered Nov 10 '22 09:11

BalusC


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

like image 41
Kasinaat 007 Avatar answered Nov 10 '22 08:11

Kasinaat 007