Java Properties类加载配置文件转义问题

探讨了使用Properties类加载配置文件时遇到的转义问题,详细分析了问题的原因,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景:
配置文件config.properties
配置项cfg.regexp=\d+\t
加载配置文件代码
InputStream ins = PropManager.class.getResourceAsStream("/config.properties");
prop.load(ins);

现象:
打印cfg.regexp的值输出为"d+ ",而不是期望的"\d+\t"

原因:
看了下代码,发现Properties类对'\'做了转义处理,而且只处理'\uxxxx', '\t','\n','\r','\f'这几种情况,对于其他情况,就简单的把'\'吞掉了

while (off < end) {
aChar = in[off++];
if (aChar == '\\') {
aChar = in[off++];
if(aChar == 'u') {
// Read the xxxx
int value=0;
for (int i=0; i<4; i++) {
aChar = in[off++];
switch (aChar) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = (value << 4) + aChar - '0';
break;
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
out[outLen++] = (char)value;
} else {
if (aChar == 't') aChar = '\t';
else if (aChar == 'r') aChar = '\r';
else if (aChar == 'n') aChar = '\n';
else if (aChar == 'f') aChar = '\f';
out[outLen++] = aChar;
}
} else {
out[outLen++] = (char)aChar;
}
}

解决方法:
我的解决方法是,自己写个简单的解析
约定
[list]
[*]每行第一次出现的'='作为分割符(Properties类可以支持key:value格式)
[*]key和value都忽略前后空白字符
[*]不对字符串做转义处理
[/list]


while((line = buffReader.readLine())!=null){
line = line.trim();
if(line.startsWith("#") || line.equals("")){// 忽略#开头的注释
continue;
}

int index = line.indexOf('=');
if(index <= 0){
logger.error("********错误的配置文件格式!********line = " + line);
continue;
}
String key = line.substring(0, index).trim();
String value = (index+1>=line.length()) ? "":line.substring(index+1).trim();// 避免越界
prop.put(key, value);
}


之所以把它记下来是因为我跟同事说起这个现象的时候,同事发现他之前写的代码用过类似的正则表达式配置方式,正则表达式里面希望匹配绝对的'\.',而经过Properties类转义之后变成了'.',意义就变成了“任意字符”,由于运行并不报错,而且某系情况下结果还是正确的,于是一个潜在的bug就出现了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值