Java 干净的字符串?

标签 java string sockets tcp

所以我正在使用 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/

相关文章:

java - Spring Roo - 将查找器添加到 list.jspx 中的每一行

java - Mysql 需要很长时间才能检索结果

string - 重载方法调用有替代 : String. 格式

php - 为什么socket read不离开循环?

java - 我希望当某些访问者加载链接(在帖子和页面上)时自动单击它

Java - if/else 在应该暂停时立即失败

string - 在 Haskell 中,如何获得 0.03 而不是 3.0e-2?

java - 如何将两个已排序的字符串数组编织成一个已排序的数组

Java NIO : Sending more than 16 objects causes error

linux - 设置不同的发送和接收内核缓冲区大小