Java 如何允许特殊字符'&';在爪哇

Java 如何允许特殊字符'&';在爪哇,java,character,Java,Character,我有以下代码来允许特殊字符: public static String replacer1(StringBuffer outBuffer) { String data = ""; StringBuffer tempBuffer = null; try { if (outBuffer != null) { data = outBuffer.toString();

我有以下代码来允许特殊字符:

public static String replacer1(StringBuffer outBuffer) {

        String data = "";
        StringBuffer tempBuffer = null;
        try {
            if (outBuffer != null) {
                data = outBuffer.toString();
                tempBuffer = new StringBuffer();
                int incrementor = 0;
                int dataLength = data.length();
                while (incrementor < dataLength) {
                    char charecterAt = data.charAt(incrementor);
                    if (charecterAt == '%') {
                        tempBuffer.append("<percentage>");
                    } else if (charecterAt == '+') {
                        tempBuffer.append("<plus>");
                    }else if (charecterAt == '-') {
                        tempBuffer.append("<hyphen>");
                    } else if (charecterAt == '&') {
                        tempBuffer.append("<AMPERSAND>");
                    }  else if (charecterAt == '_') {
                        tempBuffer.append("<UNDERSCORE>");
                    }

                    else {
                        tempBuffer.append(charecterAt);
                    }
                    incrementor++;
                }
                data = tempBuffer.toString();
                data = URLDecoder.decode(data, "utf-8");
                data = data.replaceAll("<percentage>", "%");
                data = data.replaceAll("<plus>", "+");
                data = data.replaceAll("<hyphen>", "-");
                data = data.replaceAll("<AMPERSAND>", "&");
                data = data.replaceAll("<UNDERSCORE>", "_");
                //data = data.replaceAll("\"", "");
                data = data.replaceAll("'", "");
                data = data.trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
publicstaticstringreplacer1(stringbufferextuffer){
字符串数据=”;
StringBuffer-tempBuffer=null;
试一试{
if(exputffer!=null){
data=exputffer.toString();
tempBuffer=新的StringBuffer();
int incrementor=0;
int dataLength=data.length();
while(增量<数据长度){
字符数=data.charAt(递增);
如果(字符数=='%'){
tempBuffer.append(“”);
}else if(字符数=='+'){
tempBuffer.append(“”);
}else if(字符数=='-'){
tempBuffer.append(“”);
}else if(字符数=='&'){
tempBuffer.append(“”);
}else if(字符数==''.'){
tempBuffer.append(“”);
}
否则{
tempBuffer.append(字符数);
}
Encrementor++;
}
data=tempBuffer.toString();
数据=URLDecover.decode(数据,“utf-8”);
data=data.replaceAll(“,%”);
data=data.replaceAll(“,”+”);
data=data.replaceAll(“,“-”);
data=data.replaceAll(“,”和“);
data=data.replaceAll(“,”);
//data=data.replaceAll(“\”,“);
data=data.replaceAll(“,”);
data=data.trim();
}
}捕获(例外e){
e、 printStackTrace();
}
返回数据;
}
除“&”外,所有特殊字符都将出现

还有别的办法吗?
请帮助

@Sanjeev为什么?我想在Java字符文本中,您必须转义的唯一字符是\和'@Andrewilliamson you's right.。我误解了这个问题。@OP您能澄清一下吗?如果您在while循环之后放一个print语句,您的符号是否正确转换为“”字符串?