Java 使用Apache Commons配置将用户输入的路径传递到.properties文件中
我有一个简单的GUI,弹出并要求用户输入几个字段。其中一个字段用于配置路径。我获取用户在GUI中输入的内容(JTextField),将其保存为字符串,并使用Apache Commons配置库(我使用的是PropertiesConfiguration.setProperty())根据用户输入的内容更新.properties文件。问题是,由于字符是如何转义的,所以这不起作用。如果用户输入:Java 使用Apache Commons配置将用户输入的路径传递到.properties文件中,java,swing,escaping,Java,Swing,Escaping,我有一个简单的GUI,弹出并要求用户输入几个字段。其中一个字段用于配置路径。我获取用户在GUI中输入的内容(JTextField),将其保存为字符串,并使用Apache Commons配置库(我使用的是PropertiesConfiguration.setProperty())根据用户输入的内容更新.properties文件。问题是,由于字符是如何转义的,所以这不起作用。如果用户输入: \:客户\:身份验证处理器 然后,我希望在属性文件中更新该字符串,使其如下所示: path=\:cust\:a
\:客户\:身份验证处理器
然后,我希望在属性文件中更新该字符串,使其如下所示:
path=\:cust\:authprocessor
相反,它看起来是这样的:
path=\\:cust\\:authprocessor
我尝试过使用String.replace(),但由于它们被转义,因此无法使用。有没有关于如何处理的想法?这是不可能的。是属性中的一个特殊字符。如果选择这些属性,它们将被转义
在这里你可以看到
private String saveConvert(字符串,
布尔转义空间,
布尔转义码){
int len=字符串长度();
int bufLen=len*2;
如果(bufLen<0){
bufLen=整数的最大值;
}
StringBuffer Exputffer=新的StringBuffer(bufLen);
对于(int x=0;x 61)和(aChar<127)){
如果(aChar=='\\'){
exputffer.append('\\');exputffer.append('\\');
继续;
}
外加剂(aChar);
继续;
}
开关(aChar){
案例“”:
如果(x==0 | | escapeSpace)
exputffer.append('\\');
附加(“”);
打破
大小写“\t”:exputffer.append(“\\”);exputffer.append('t');
打破
大小写“\n”:exputffer.append(“\\”);exputffer.append('n');
打破
大小写“\r”:exputffer.append(“\\”);exputffer.append('r');
打破
大小写'\f':exputffer.append('\\');exputffer.append('f');
打破
案例“=”://失败
案例“:”://失败
案例“#”://失败
案例“!”:
exputffer.append('\\');exputffer.append(aChar);
打破
违约:
if((亚喀尔<0x0020)| |(亚喀尔>0x007e))&escapeUnicode){
exputffer.append('\\');
exputffer.append('u');
exputffer.append(toHex((aChar>>12)和0xF));
exputffer.append(toHex((aChar>>8)和0xF));
exputffer.append(toHex((aChar>>4)和0xF));
exputffer.append(toHex(aChar&0xF));
}否则{
外加剂(aChar);
}
}
}
返回exputfer.toString();
}
我认为在文件\
中将显示为\
,但在(java)程序中读取时,它将显示为\
。你能尝试一下/
?我会记住这一点,但是用户已经习惯了以这种方式输入它:“\:cust\:authprocessor”,所以即使这样做有效,也很难让一群人改变他们习惯于输入的方式,因为这个应用程序应该让他们的生活更轻松。有没有什么方法可以让我在插入后轻松修改?你检查了吗?@一个陌生人-没有用。如果我传入字符串“\\:cust\\:authprocessor”,它将返回“\\:cust\\:authprocessor”。同样的事情。在同一线程中使用Apache Commons one会导致“:cust:authprocessor”。我找不到从GUI获取单个反斜杠并将其保存为属性的方法。是否建议您扩展该类并更改转义规则的实现方式?…或者只是说明为什么它不可能:-)@Trinculo我将向您说明java.util.Properties
的标准实现是不可能的。
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();
}