在尝试将inputStream字符串与另一个字符串进行比较后,无论我做什么(.equals,.compareTo),它们都不相等,即使它们是。inputStream字符串与字符串字面值不同
String readString = "READ";
System.out.println(method + " =? " + readString);
if (method.trim().equalsIgnoreCase(readString)) {
System.out.println("Is READ");
} else {
System.out.println("Is not READ");
}
它打印出来,
READ =? READ
Is not READ
即使方法变量肯定是阅读。
然后我发送了一个我的朋友有同样的问题,他说这是因为inputStream我解析了方法变量。
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream()
, "UTF8"
)
);
StringBuilder arrInput = new StringBuilder();
char[] cbuf = new char[DEFAULT_BUFFER_SIZE];
int bytesRead;
// Build client's input and parse it
while (true) {
bytesRead = inFromClient.read(cbuf);
arrInput.append(cbuf);
...
// lots of parsing below and then a break
}
我已经尽我的朋友的解决方案:String created from inputStream is not equal to its string literal
但我最终只获得了输入我读的一部分,不像他做到了。
请把代码放在第一个块上的方法 – rupps 2014-11-02 03:24:21
你应该只从'cbuf'中追加'bytesRead'字节。 – 2014-11-02 03:45:46