前两天写的程序,今天发现有个bug,超过一定长度的字符串在保存到配置文件中时变成了balabala...这样的形式。
调试代码发现原来是Properties.list(PrintStream out)
方法会将value长度大于40的截取前37个字符加上...作为保存的实际值。
/**
* Prints this property list out to the specified output stream.
* This method is useful for debugging.
*
* @param out an output stream.
* @throws ClassCastException if any key in this property list
* is not a string.
*/
public void list(PrintStream out) {
out.println("-- listing properties --");
Hashtable h = new Hashtable();
enumerate(h);
for (Enumeration e = h.keys() ; e.hasMoreElements() 😉 {
String key = (String)e.nextElement();
String val = (String)h.get(key);
if (val.length() > 40) {
val = val.substring(0, 37) + "...";
}
out.println(key + "=" + val);
}
}
后来发现更常用的是Properties.store(OutputStream out, String comments)
方法,果然替换之后就没这问题了。
可怜我压根没怎么看Properties类的相关内容,随便找个例子修修改改就放上去了,结果遇到各种问题,这种教训也是要记住的。