ReadLine()问题

虽然写IO方面的程序不多,但BufferedReader/BufferedInputStream倒是用过好几次的,原因是:

  • 它有一个很特别的方法:readLine(),使用起来特别方便,每次读回来的都是一行,省了很多手动拼接buffer的琐碎;
  • 它比较高效,相对于一个字符/字节地读取、转换、返回来说,它有一个缓冲区,读满缓冲区才返回;一般情况下,都建议使用它们把其它Reader/InputStream包起来,使得读取数据更高效。
  • 对于文件来说,经常遇到一行一行的,特别相符情景。

这次是在蓝牙开发时,使用两个蓝牙互相传数据(即一个发一个收),bluecove这个开源组件已经把数据读取都封装成InputStream了,也就相当于平时的IO读取了,很自然就使用起readLine()来了。

发数据:

[java]

view plain

copy

<embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box; animation-duration: 0.001s; animation-name: playerInserted;">

  1. BufferedWriter output =

    new

    BufferedWriter(

    new

    OutputStreamWriter(conn.openOutputStream()));

  2. int

    i =

    1

    ;

  3. String message =

    "message "

    • i;
  4. while

    (isRunning) {

  5. output.write(message+

    "/n"

    );

  6. i++;

  7. }

读数据:

[java]

view plain

copy

<embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box; animation-duration: 0.001s; animation-name: playerInserted;">

  1. BufferedReader input =

    new

    BufferedReader(

    new

    InputStreamReader(m_conn.openInputStream()));

  2. String message =

    ""

    ;

  3. String line =

    null

    ;

  4. while

    ((line = m_input.readLine()) !=

    null

    ) {

  5. message += line;

  6. }

  7. System.out.println(message);

上面是代码的节选,使用这段代码会发现写数据时每次都成功,而读数据侧却一直没有数据输出(除非把流关掉)。经过折腾,原来这里面有几个大问题需要理解:

  • 误以为readLine()是读取到没有数据时就返回null(因为其它read方法当读到没有数据时返回-1),而实际上readLine()是一个阻塞函数,当没有数据读取时,就一直会阻塞在那,而不是返回null;因为readLine()阻塞后,System.out.println(message)这句根本就不会执行到,所以在接收端就不会有东西输出。要想执行到System.out.println(message),一个办法是发送完数据后就关掉流,这样readLine()结束阻塞状态,而能够得到正确的结果,但显然不能传一行就关一次数据流;另外一个办法是把System.out.println(message)放到while循环体内就可以。
  • readLine()只有在数据流发生异常或者另一端被close()掉时,才会返回null值。
  • 如果不指定buffer大小,则readLine()使用的buffer有8192个字符。在达到buffer大小之前,只有遇到"/r"、"/n"、"/r/n"才会返回。

readLine()的实质(下面是从JDK源码摘出来的):

[java]

view plain

copy

<embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box; animation-duration: 0.001s; animation-name: playerInserted;">

  1. String readLine(

    boolean

    ignoreLF)

    throws

    IOException {

  2. StringBuffer s =

    null

    ;

  3. int

    startChar;

  4. synchronized

    (lock) {

  5. ensureOpen();

  6. boolean

    omitLF = ignoreLF || skipLF;

  7. bufferLoop:

  8. for

    (;;) {

  9. if

    (nextChar >= nChars)

  10. fill();

//在此读数据
  1. if

    (nextChar >= nChars) {

/* EOF */
  1. if

    (s !=

null

 && s.length() > 

0

)
  1. return

    s.toString();

  2. else

  3. return

null

;
  1. }

  2. ......

//其它
  1. }

  2. private

void

 fill() 

throws

 IOException {
  1. ..../其它

  2. int

    n;

  3. do

    {

  4. n = in.read(cb, dst, cb.length - dst);

//实质
  1. }
while

 (n == 

0

);
  1. if

    (n >

0

) {
  1. nChars = dst + n;

  2. nextChar = dst;

  3. }

  4. }

从上面看出,readLine()是调用了read(char[] cbuf, int off, int len) 来读取数据,后面再根据"/r"或"/n"来进行数据处理。

在Java I/O书上也说了:

public String readLine() throws IOException
This method returns a string that contains a line of text from a text file. /r, /n, and /r/n are assumed to be line breaks and are not included in the returned string. This method is often used when reading user input from System.in, since most platforms only send the user's input to the running program after the user has typed a full line (that is, hit the Return key).
readLine() has the same problem with line ends that DataInputStream's readLine() method has; that is,

the potential to hang on a lone carriage return that ends the stream

. This problem is especially acute on networked connections, where readLine() should never be used.

小结,使用readLine()一定要注意:

  1. 读入的数据要注意有/r或/n或/r/n
  2. 没有数据时会阻塞,在数据流异常或断开时才会返回null
  3. 使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,872评论 18 399
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,419评论 11 349
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,792评论 0 33
  • 不管是B2B,C2C,在有交易的电子商务过程当中,不可避免的出现促销这样的事,一般常见的促销包括打折优惠,买多少减...
    breezedancer阅读 1,225评论 0 48
  • 本打算昨天结束掉最后一期简历书写分享。 可自从前几天爱趣丫写了一期《应届毕业生,简历不可太花哨》之后,部分应届生弟...
    爱趣丫阅读 1,148评论 2 11