4
votes
  • 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");
    }
  }
}
4
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

4 Answers

7
votes

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
3
votes

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();
0
votes

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.

0
votes

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();
    }