在嘗試將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