Delete key and value from a property file?

前端 未结 2 674
[愿得一人]
[愿得一人] 2021-01-17 07:41

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

2条回答
  •  心在旅途
    2021-01-17 08:12

    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

提交回复
热议问题