Search code examples
javapropertiesproperties-file

Delete key and value from properties file using java


  • I have some inserted values in my properties file. which will be like,

Key=Value

  • I have to update the properties file. while updating, am checking whether the key is available. if key is there, i need to delete the key and value and have to write it again.
  • Can anyone give me the code to delete the existing key and value before updating/writing it again.

here is my java code to insert and update:

 if (action.equals("insert")) {
  if (con != null) {
    if (key == null) {
      //session.setAttribute(username, con); 
      out.println("****Connected Successfully****");
      String rootPath=request.getSession().getServletContext().getRealPath("/");
      System.out.println(rootPath);
      String propPath=rootPath+"/WEB-INF/";
      PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true)));
      out1.println(cname+"=jdbc:oracle:thin:@"+host+":"+port+"/"+service+","+username+","+password);
      out1.close();
    } else {         
      out.println("*Connection name "+cname+" already exists. Please try with another name");
    }
  }
}
if (action.equals("update")) {
  if (con != null) {
    if (key == null) {
      out.println(cname+" is not available.");
    } else {
      String rootPath=request.getSession().getServletContext().getRealPath("/");
      System.out.println(rootPath);
      String propPath=rootPath+"/WEB-INF/";
      PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true)));
      out1.println(cname+"=jdbc:oracle:thin:@"+host+":"+port+"/"+service+","+username+","+password);
      out1.close();
      out.println("updated successfully");
    }
  }
}

Solution

  • Load it:

    Properties properties = new Properties();
    properties.load(your_reader);
    

    Then use the remove() method to remove a property:

    properties.remove(your_key);
    

    And finally write this change to the file properties file:

    properties.store(your_writer, null);
    

    UPDATE

    I post this update after your comment:

    i tried.. what am getting is, its not disturbing the older content.. just rewriting the file again with deleted value.. initially the file had key1=value1 and key2=value2.. using the above code, i tried to remove key2, now the file has, key1=value1 key2=value2 then today's time and date after that key1=value1

    Try with this example, i tried and it works perfectly, i think you have some error in your code if it doesn't work:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.util.Properties;
    
    public class Props {
    
        public Props() {
            try {
                File myFile = new File("props.properties");
                Properties properties = new Properties();
                properties.load(new FileInputStream(myFile));
                properties.remove("deletethis");
                OutputStream out = new FileOutputStream(myFile);
                properties.store(out, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[]args){
            new Props();
        }
    }
    

    props.properties before:

    #Wed Mar 06 11:15:24 CET 2013
    file=File
    edit=Edit
    deletethis=I'm gonna be deleted!
    

    props.properties after:

    #Wed Mar 06 11:15:24 CET 2013
    file=File
    edit=Edit