使用Java和readLine()回车和换行 [英] Carriage return and new line with Java and readLine()

查看:2758
本文介绍了使用Java和readLine()回车和换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码建立HTTP连接并读取数据:

I am using the following code to establish a HTTP connection and read data:

con = (HttpURLConnection) new URL("http://stream.twitter.com/1/statuses/sample.json").openConnection();
...
con.connect();
while (line = rd.readLine()) {
    if (line.contains("\r\n")) {
      System.out.println("Carriage return + new line");
    }
} 

然而,似乎是\\\ n不是字符串的一部分(),尽管服务器确实返回它们。如何读取数据并检测\\ n?

However, it seems like "\r\n" is not part of the string (line), although the server does return them. How can I read the data and detect "\r\n"?

谢谢,

Joel

推荐答案

如果 rd 的类型为 BufferedReader 无法判断 readLine()是否返回以 \ n \ r \\\\ n ...行尾字符是丢弃而不是返回字符串的一部分。

If rd is of type BufferedReader there is no way to figure out if readLine() returned something that ended with \n, \r or \r\n... the end-of-line characters are discarded and not part of the returned string.

如果您真的关心这些字符,则无法通过 readLine()。例如,您必须通过 read()

If you really care about these characters, you can't go through readLine(). You'll have to for instance read the characters one by one through read().

这篇关于使用Java和readLine()回车和换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆