[英]Remove comments in properties file java
when i updated the properties file the comments are also updated along the data .is there any possible way to remove the comments or updating the data without comment. 当我更新属性文件时,注释也会随数据一起更新。是否有任何可能的方式来删除注释或更新无注释的数据。
Here i update the file 4 times each time the date-time stamp append as a comment 在这里,我每次将日期时间戳添加为注释时都会对文件进行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
code 码
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);
I once had to do this because a consumer of the properties file couldn't handle the properties. 我曾经不得不这样做,因为属性文件的使用者无法处理属性。 The comment produced by the store
method is well defined, so it's easy enough to skip over it: 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()));
Here's an improvement of the hack above which uses the right encoding as well. 这是对上述技巧的改进,它也使用了正确的编码。
FileOutputStream out = new FileOutputStream(filename);
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
props.store(arrayOut, null);
String string = new String(arrayOut.toByteArray(), "8859_1");
String sep = System.getProperty("line.separator");
String content = string.substring(string.indexOf(sep) + sep.length());
out.write(content.getBytes("8859_1"));
From the JavaDocs for properties.store() 从JavaDocs获取properties.store()
If the comments argument is not null, then an ASCII # character, the comments string, and a line separator are first written to the output stream. 如果comment参数不为null,则首先将ASCII#字符,注释字符串和行分隔符写入输出流。 Thus, the comments can serve as an identifying comment. 因此,评论可以用作识别评论。
Next, a comment line is always written, consisting of an ASCII # character, the current date and time (as if produced by the toString method of Date for the current time), and a line separator as generated by the Writer. 接下来,总是写一条注释行,其中包括一个ASCII#字符,当前日期和时间(好像是由Date的toString方法为当前时间生成的)和由Writer生成的行分隔符。
The only option I can think of would be to write your ownOutputStream implementation to drop the comments. 我唯一想到的选择是编写自己的OutputStream实现以删除注释。 ( Or just learn to live with them :) ) (或者只是学习与他们生活在一起:))
In groovy I wrote this to entirely remove comments from the Properties. 通常,我写这篇文章是为了完全删除属性中的注释。
String writeProperties(Properties properties) {
def writer = new StringWriter()
properties.each { k, v ->
writer.write("${k}=${v}${System.lineSeparator()}")
}
writer.toString()
}
This should be easy to convert to regular java. 这应该很容易转换为常规java。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.