gpt4 book ai didi

java - 属性类和转义字符

转载 作者:行者123 更新时间:2023-11-30 03:17:05 25 4
gpt4 key购买 nike

我使用属性类将 map 存储到文件中。但有些字符是在开头加“\”存储的。

是否有任何方法可以使用 Properties 类打印实际内容,而无需这些转义。

我正在使用属性类进行写入,但使用 BufferedReader 进行读取。我无法理解使用 Properties Class 编写时哪些字符被转义。

有没有办法使用 BufferedReader 读取数据而无需这些转义?

例如:

如果实际内容是abc:def:ghi当我将此值分配给名为 name 的属性并使用属性类将其存储在文件中时,它存储为:

name=abc\:def\:ghi.

现在,当我使用 BufferedReader 阅读此内容时,我也得到了内容中的所有\字符。

问题是我不知道所有字符都通过添加的\字符存储。

最佳答案

所有特殊字符均由 java.util.Properties 转义。您无法提供它。

将于 saveConvert 完成:

   /*
* Converts unicodes to encoded \uxxxx and escapes
* special characters with a preceding slash
*/
private String saveConvert(String theString,
boolean escapeSpace,
boolean escapeUnicode) {
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = Integer.MAX_VALUE;
}
StringBuffer outBuffer = new StringBuffer(bufLen);

for(int x=0; x<len; x++) {
char aChar = theString.charAt(x);
// Handle common case first, selecting largest block that
// avoids the specials below
if ((aChar > 61) && (aChar < 127)) {
if (aChar == '\\') {
outBuffer.append('\\'); outBuffer.append('\\');
continue;
}
outBuffer.append(aChar);
continue;
}
switch(aChar) {
case ' ':
if (x == 0 || escapeSpace)
outBuffer.append('\\');
outBuffer.append(' ');
break;
case '\t':outBuffer.append('\\'); outBuffer.append('t');
break;
case '\n':outBuffer.append('\\'); outBuffer.append('n');
break;
case '\r':outBuffer.append('\\'); outBuffer.append('r');
break;
case '\f':outBuffer.append('\\'); outBuffer.append('f');
break;
case '=': // Fall through
case ':': // Fall through
case '#': // Fall through
case '!':
outBuffer.append('\\'); outBuffer.append(aChar);
break;
default:
if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
outBuffer.append('\\');
outBuffer.append('u');
outBuffer.append(toHex((aChar >> 12) & 0xF));
outBuffer.append(toHex((aChar >> 8) & 0xF));
outBuffer.append(toHex((aChar >> 4) & 0xF));
outBuffer.append(toHex( aChar & 0xF));
} else {
outBuffer.append(aChar);
}
}
}
return outBuffer.toString();
}

关于java - 属性类和转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32312500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com