java - 删除属性文件java中的注释

标签 java

当我更新属性文件时,注释也会随着数据一起更新。是否有任何可能的方法来删除注释或更新数据而不带注释。

在这里,每次将日期时间戳附加为注释时,我都会更新文件 4 次

#Thu May 19 17:53:42 GMT+05:30 2011
Key_1=DSA_1024
#Thu May 19 17:53:43 GMT+05:30 2011
Key_2=DSA_1024
#Thu May 19 17:53:43 GMT+05:30 2011
Key_3=DSA_1024
#Thu May 19 17:53:44 GMT+05:30 2011
Key_4=DSA_1024

代码

  Properties prop=new Properties();
            String currentDirectary=System.getProperty("user.dir");
            String path=currentDirectary+"/Resource/Key.Properties";
            FileOutputStream out=new FileOutputStream(path,true);
            prop.setProperty("keyName","DSA_1024");
            prop.store(out, null);

最佳答案

我曾经不得不这样做,因为属性文件的使用者无法处理这些属性。 store 方法生成的注释定义明确,因此很容易跳过它:

Writer stringOut = new StringWriter();
properties.store(stringOut, null);
String string = stringOut.toString();
String sep = System.getProperty("line.separator");
out.write(string.substring(string.indexOf(sep) + sep.length()));

关于java - 删除属性文件java中的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058638/

相关文章:

java - 如何打开和关闭文件编写器附加?

java - 从数组列表创建 Swing 树

java - Spring FeignClient 回退未调用但进入异常

java - 对 hsqldb 立即执行

java - 从启动的程序中获取 JNLP 文件名

java - 这种 sleep 的实现有什么问题吗?

java - SAXParseException XML-20221 文本中的无效字符

Java Socket - read() 方法如何知道是否已到达流的末尾?

java - DialogFragment 上的 ArrayIndexOutOfBoundsException 崩溃

java - 使用 java 调用 SOAP webservice