Take the 2-minute tour ×

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 for a line to exist, thus halting the entire code.

if((inputLine = bufferedReader.readLine()) != null){
                JOptionPane.showMessageDialog(null, inputLine);

Is there a way to better check if there is text in a BufferedReader to be read?

share|improve this question
Why? It will block if there's no data. What else are you going to do instead? –  EJP Nov 28 '12 at 1:14
Have you tried BufferedReader#ready() docs.oracle.com/javase/6/docs/api/java/io/…? –  reprogrammer Nov 28 '12 at 1:15
I don't think that ready() would answer that need. ready() would tell him whether there's something in the buffer; it won't tell him whether there's a full line to read. readLine() will still enter a waiting state, won't it? –  Isaac Nov 28 '12 at 1:18
ready fixed it, thanks guys –  user1729831 Nov 28 '12 at 1:25

3 Answers 3

up vote 0 down vote accepted

No, there's no easy way to do that. BufferedReader has a ready call, but that only applies to the read calls, not the readLine call. If you really want a readLine that's guaranteed not to block, you must implement it yourself using read and maintaining a char buffer yourself.

share|improve this answer
so if I do ready() that will skip over it if there is no text in there? the readlLine was just so I could use spaces. –  user1729831 Nov 28 '12 at 1:16

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:


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.

share|improve this answer

Why don't you check if it's ready to be read first? Just use bufferedReader.ready().


ready won't tell you if you have a line ready, it will just tell you that there is something to be read. However, if you are expecting to get a line then this will work for you. The idea would be, first check if it's ready, and then read the line, in this way the thread won't be hanging there when there is absolutely nothing to read.

share|improve this answer
ready() will return true but readLine() might still block, if the buffer contains characters none of which is a line terminator. –  Isaac Nov 28 '12 at 1:25
It's not reliable in general (allowed to give false negatives), and in any case, only checks that there's at least one character available, not a full line. –  erickson Nov 28 '12 at 1:26

Your Answer


