使用java从属性文件中删除键和值

Rac*_*hel 4 java properties properties-file

  • 我的属性文件中有一些插入的值.会是这样的,

键=值

  • 我必须更新属性文件.在更新时,检查密钥是否可用.如果有密钥,我需要删除密钥和值,并且必须再次写入.
  • 在更新/再次写入之前,任何人都可以给我删除现有密钥和值的代码.

这是我要插入和更新的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");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Bac*_*ash 6

加载它:

Properties properties = new Properties();
properties.load(your_reader);
Run Code Online (Sandbox Code Playgroud)

然后使用remove()方法删除属性:

properties.remove(your_key);
Run Code Online (Sandbox Code Playgroud)

最后将此更改写入文件属性文件:

properties.store(your_writer, null);
Run Code Online (Sandbox Code Playgroud)

UPDATE

我发表评论后发布此更新:

我试过..得到的是什么,它不会扰乱旧内容..只是用删除值重写文件..最初文件有key1 = value1和key2 = value2 ..使用上面的代码,我试图删除key2 ,现在文件有,key1 = value1 key2 = value2然后今天的时间和日期后key1 = value1

试试这个例子,我试过,它运行得很完美,我认为你的代码中有一些错误,如果它不起作用:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

props.properties 之前:

#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit
deletethis=I'm gonna be deleted!
Run Code Online (Sandbox Code Playgroud)

props.properties 之后:

#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit
Run Code Online (Sandbox Code Playgroud)