[英]Problem in writing special characters to a properties file in Java
我需要将键值对添加到属性文件中。 除了# and = everytine a \ 附加在字符之前,所有工作都正常。
请与我分享任何建议。
当前属性文件数据
纸=归一化
我想评论这个键
#paper = 标准化
但正在发生的事情是 \ 正在添加
\#paper = 标准化
'''
String valueOfKey = updatedMap.get(key);
updatedMap.remove(key);
updatedMap.put("#" + key, valueOfKey);
String totalPath = propertiesService.getFilePath(request) + "\\" + propertiesModel.getSelectedFile();
propertiesService.updatePropertyfile(updatedMap, request, totalPath);
'''
'''
public boolean updatePropertyfile(Map<String, String> map, HttpServletRequest request, String fileName) {
Properties props = new Properties();
Writer Out = null;
File file = new File(fileName);
try {
FileOutputStream out = new FileOutputStream(file);
Out = new BufferedWriter(new OutputStreamWriter(out));
Set<String> keyset = map.keySet();
Iterator iter = keyset.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
props.setProperty(key, (String) map.get(key));
}
props.store(Out, "update");
Out.flush();
Out.close();
} catch (IOException e) {
return false;
}
return true;
}
'''
属性文件中的值被写入
hash 标签是 Java 属性文件中注释的导入:
# Created by generator on 2020-05-01
#current properties file data
paper = Normalised
#want to update like
#paper = Normalised – but this is a comment …
#but what is happening is \# is getting added
\#paper = Normalised # Backslash required …
所以用反斜杠转义是让它工作的唯一方法。
不幸的是,当您需要使用另一个 API 而不是java.util.Properties
来读取属性文件时,您必须将此功能添加到解析器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.