4
  • 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");
    }
  }
}
Martin Schneider
  • 3,268
  • 4
  • 19
  • 29
Rachel
  • 1,131
  • 11
  • 26
  • 43
  • I would use java.util.properties. Apologies for the formatting, I am writing this on an iPhone and return saves my response rather than giving a new line. :(. Example code is : Properties p = new Properties(); p.load(inputStream); p.getProperty("key"); p.setProperty("key","value"); p.store(outputStream, "comments"); – Chris K Mar 06 '13 at 06:10
  • Possible duplicate of [Delete key and value from a property file?](http://stackoverflow.com/questions/4225794/delete-key-and-value-from-a-property-file) – Martin Schröder May 18 '17 at 11:30

4 Answers4

7

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
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 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 – Rachel Mar 06 '13 at 06:24
  • "properties.store(out, null);" is this the only way to write into property file? – Rachel Mar 06 '13 at 10:37
  • There is also a `save()` method, but it's deprecated – BackSlash Mar 06 '13 at 10:45
  • in properties file, the key and value should be in this format.. "key = jdbc:dbname:dbtype:host:port/SID,uname,pass".. but while writing with store() method, its adding escape sequences.. so now the key and value "key = jdbc\:dbname\:dbtype\:host\:port/SID,uname,pass"... how to write it without escape sequences.. – Rachel Mar 06 '13 at 11:37
  • i tried with your string and everything is ok, are you sure you are using a properly-working jdk? – BackSlash Mar 06 '13 at 12:23
  • i solved it just now... its working fine for me.. instead store, i tried writing with this code... PrintWriter pw = new PrintWriter("test.properties"); for(Entry e : props.entrySet()) { pw.println(e); } pw.close(); – Rachel Mar 06 '13 at 12:31
3

Have you looked at Apache Commons Configuration?

I would try something like:

import org.apache.commons.configuration.PropertiesConfiguration;

PropertiesConfiguration config = new PropertiesConfiguration("myfile.properties");    
config.clearProperty("my.property");
config.save();
Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
user3229382
  • 81
  • 1
  • 4
0

The simplest and most obvious solution is to read your input file, go through it line-by-line and check for properties you need to replace. Then do whatever changes you need and rewrite the entire file.

You might also want to use a temporary file to write your new config and then replace the existing one with it. This will help you in case there is a chance of your app crashing in the middle of process. You'll still have a working old config at place.

svz
  • 4,516
  • 11
  • 40
  • 66
0

try

    Properties props = new Properties();
    FileInputStream in = new FileInputStream(file);
    props.load(in);
    in.close();
    if (props.remove("key") != null) {
        FileOutputStream out = new FileOutputStream(file);
        props.store(out, "");
        out.close();
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 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 – Rachel Mar 06 '13 at 06:28
  • it definitely works, you need to debug it, eg replace props.store(out, ""); with props.store(System.out, ""); and see what's going on – Evgeniy Dorofeev Mar 06 '13 at 06:37