BufferedReader, detecting if there is text left to read

后端 未结 3 1882
盖世英雄少女心
盖世英雄少女心 2021-01-06 00:33

I\'m running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting

3条回答
  •  抹茶落季
    2021-01-06 01:07

    The readLine method will block unless it can read an entire line (delimited by a line-terminating character), or the end of input has been reached:

    http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()

    The case you're describing is that the end of input hasn't yet been reached, but there's not enough characters in the buffer to constitute a "line" (that is, a series of characters ended by a line terminator).

    You will have to go lower than the readLine level for that, possibly to the Stream level itself. InputStream has a method called available(), which would answer your needs.

提交回复
热议问题