[英]Delete key and value from properties file using java
Key=Value
键=值
here is my java code to insert and update: 这是我要插入和更新的java代码:
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: 然后使用remove()方法删除属性:
properties.remove(your_key);
And finally write this change to the file properties file: 最后将此更改写入文件属性文件:
properties.store(your_writer, null);
UPDATE 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
我试过..得到的是什么,它不会扰乱旧内容..只是用删除值重写文件..最初文件有key1 = value1和key2 = value2 ..使用上面的代码,我试图删除key2 ,现在文件有,key1 = value1 key2 = value2然后今天的时间和日期后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 : props.properties 之前 :
#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit
deletethis=I'm gonna be deleted!
props.properties after : props.properties 之后 :
#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit
Have you looked at Apache Commons Configuration ? 你看过Apache Commons配置了吗?
I would try something like: 我会尝试类似的东西:
import org.apache.commons.configuration.PropertiesConfiguration;
PropertiesConfiguration config = new PropertiesConfiguration("myfile.properties");
config.clearProperty("my.property");
config.save();
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.
您仍然可以使用旧的配置。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.