所以我正在使用 TCP,并从 DataInputStream 获取数据,但是当我获取数据时,它周围有 ^X^@^E^R 之类的内容。当它不应该做的时候。无论如何我可以删除这个吗?但它需要是动态的。
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
StringBuffer inputLine = new StringBuffer();
String tmp;
while((tmp = dataIn.readLine()) != null){
//tmp = Normalizer.normalize(tmp, Normalizer.Form.NFD);
inputLine.append(tmp);
logText(tmp);
}
最佳答案
DataInputStream.readLine()
已弃用。请改用 BufferedReader。
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
我如何阅读行:
while ((socket != null) && !(socket.isClosed())) {
// read in a line but only if there is one - blocks here till there is a line
String clientSentence = input.readLine();
if ((clientSentence != null) && (!(clientSentence.equals("")))) {
// do something
}
}
关于Java 干净的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445932/