ssh项目,Properties类修改*.properties配置文件时,“:”和“=”被转义怎么解决?

Bingorl 2016-04-24 10:40:51
我项目中需要做国际化,数据库我写了中文和英文两个版本的,我在用properties类读取修改数据库连接配置文件时,数据库配置中:和=被转义成 \: 和 \= ,怎么解决?


修改配置文件的action代码:
public void updatePropertiy(String language) throws IOException{
String ConfigPath="/WEB-INF/classes/";//服务器上配置文件地址
String path=ServletActionContext.getServletContext().getRealPath(ConfigPath);
File file=new File(path,"jdbc.properties");
if(file.exists()){
System.out.println("文件存在,路径为:"+path);
}
InputStream inputStream=new FileInputStream(file);
Properties properties=new Properties();
properties.load(inputStream);
inputStream.close();
OutputStream outputStream=new FileOutputStream(file);
String lanString="";
if(language.equals("en_US")){
properties.setProperty("jdbc.url","jdbc:mysql://dev.loforce.cn:3306/official_en?characterEncoding=utf-8");
lanString="English";
}else{
properties.setProperty("jdbc.url","jdbc:mysql://dev.loforce.cn:3306/official?characterEncoding=utf-8");
lanString="Chinese";
}
properties.store(outputStream,"Database Language:"+lanString);
outputStream.close();
}

数据库连接配置文件url部分代码

修改操作前的代码:url=jdbc:mysql://dev.loforce.cn:3306/official?characterEncoding=utf-8

修改操作后的代码:url=jdbc\:mysql\://dev.loforce.cn\:3306/official?characterEncoding\=utf-8

...全文
576 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
白虹李李 2016-05-07
  • 打赏
  • 举报
回复
你要理解一个问题,如果properties写到文件中的字符串不包括转义符,那么你就可能无法使用properties来正确的读取它。 如果你要使用properties,就必须接受这种不符合阅读习惯的保存方式。 否则,你就需要自己来处理转义,比如通过replace等String的处理。 另外: 你这个例子里,如果不包括转义符可能properties也能正确读取,但是你想想如果是下面的例子: file=D:\test 如果保存下来不包括转义符,读取出来系统会认为\t是一起的,打印出来会是这样: D: est
Bingorl 2016-05-06
  • 打赏
  • 举报
回复
请问如何反编码?
attilax 2016-04-26
  • 打赏
  • 举报
回复
反编码即可。
  • 打赏
  • 举报
回复
自己用IO流拼一个出来不就行了,反正你都一行一行的去设置,代码量上还差不多,还少用一个properties类。

81,111

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧