Delete key and value from properties file using java

  • I have some inserted values ​​in the properties file. What will happen,

Key = value

  • I need to update the properties file. when updating, I check if the key is available. if I have a key, I need to delete the key and value and write it again.
  • Can someone give me a code to delete an existing key and value before updating / writing it.

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
source share
4 answers

Download it:

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

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

 properties.remove(your_key); 

And finally, write this change to the file properties file:

 properties.store(your_writer, null); 

UPDATE

I am posting this update after your comment:

I tried .. what I get does not interfere with the older content .. just rewrote the file again with the value deleted .. initially the file had key1 = value1 and key2 = value2 .. using the above code, I tried to delete key2, now the file has key1 = value1 key2 = value2, and then today's time and date after that key1 = value1

Try this example, I tried, and it works fine, I think you have some error in the 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 to :

 #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 
+6
source

Have you looked at the 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(); 
+2
source

The simplest and most obvious solution is to read your input file, go through it in turn and check the properties you need to replace. Then make all the changes and rewrite the entire file.

You can also use a temporary file to write a new configuration, and then replace the existing one. This will help you if an application crashes in your process. You will still have a working old config.

0
source

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

All Articles