【问题标题】:Delete key and value from properties file using java使用java从属性文件中删除键和值
【发布时间】:2013-02-20 19:35:33
【问题描述】:
  • 我的属性文件中有一些插入的值。就像,

键=值

  • 我必须更新属性文件。更新时,正在检查密钥是否可用。如果 key 存在,我需要删除 key 和 value 并重新写入。
  • 谁能给我代码来删除现有的键和值,然后再更新/写入。

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

【问题讨论】:

  • 我会使用 java.util.properties。为格式化道歉,我在 iPhone 上写这个并返回保存我的回复而不是换行。 :(。示例代码是:Properties p = new Properties(); p.load(inputStream); p.getProperty("key"); p.setProperty("key","value"); p.store(outputStream, "cmets");

标签: java properties properties-file


【解决方案1】:

加载它:

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

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

properties.remove(your_key);

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

properties.store(your_writer, null);

更新

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

我试过..得到的是,它不会干扰旧内容..只是用删除的值再次重写文件..最初文件有 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

【讨论】:

  • 我试过..得到的是,它不会干扰旧内容..只是用删除的值再次重写文件..最初文件有 key1=value1 和 key2=value2.. 使用上面的代码,我试图删除key2,现在文件有,key1=value1 key2=value2 然后是key1=value1之后的今天的时间和日期
  • "properties.store(out, null);"这是写入属性文件的唯一方法吗?
  • 还有一个save() 方法,但是已经弃用了
  • 在属性文件中,键和值应采用这种格式.. "key = jdbc:dbname:dbtype:host:port/SID,uname,pass".. 但在使用 store( ) 方法,它添加了转义序列.. 所以现在键和值“key = jdbc\:dbname\:dbtype\:host\:port/SID,uname,pass”...如何在没有转义序列的情况下编写它..
  • 我用你的字符串试过了,一切正常,你确定你使用的是正常工作的 jdk 吗?
【解决方案2】:

你看过Apache Commons Configuration吗?

我会尝试类似:

import org.apache.commons.configuration.PropertiesConfiguration;

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

【讨论】:

    【解决方案3】:

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

    您可能还想使用一个临时文件来编写您的新配置,然后用它替换现有的。如果您的应用程序有可能在过程中崩溃,这将为您提供帮助。您仍然可以使用旧配置。

    【讨论】:

      【解决方案4】:

      试试

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

      【讨论】:

      • 我试过..得到的是,它不会干扰旧内容..只是用删除的值再次重写文件..最初文件有 key1=value1 和 key2=value2.. 使用上面的代码,我试图删除key2,现在文件有,key1=value1 key2=value2 然后是key1=value1之后的今天的时间和日期
      • 它确实有效,你需要调试它,例如替换 props.store(out, ""); with props.store(System.out, "");看看发生了什么
      猜你喜欢
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2013-02-20
      • 1970-01-01
      相关资源
      最近更新 更多