繁体   English   中英

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

[英]Delete key and value from properties file using java

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

键=值

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

这是我要插入和更新的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");
    }
  }
}

加载它:

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

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

properties.remove(your_key);

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

properties.store(your_writer, null);

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

props.properties 之前

#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit
deletethis=I'm gonna be deleted!

props.properties 之后

#Wed Mar 06 11:15:24 CET 2013
file=File
edit=Edit

你看过Apache Commons配置了吗?

我会尝试类似的东西:

import org.apache.commons.configuration.PropertiesConfiguration;

PropertiesConfiguration config = new PropertiesConfiguration("myfile.properties");    
config.clearProperty("my.property");
config.save();

最简单,最明显的解决方案是读取输入文件,逐行检查并检查需要替换的属性。 然后执行您需要的任何更改并重写整个文件。

您可能还希望使用临时文件来编写新配置,然后用它替换现有配置。 如果您的应用有可能在流程中间崩溃,这将有助于您。 您仍然可以使用旧的配置。

尝试

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM