Key=Value
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");
}
}
}
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With