The Reader.ready() and InputStream.available() rarely work as you might like, and I don't suggest you use them. To read a file you should use
|
http://stackoverflow.com/questions/5244839/dose-bufferedreader-ready-method-ensure-that-readline-method-does-not-return
BufferedReader reader=null;
try {
reader = new BufferedReader(new StringReader("ABCD"));
while (reader.ready()) {
final String line = reader.readLine();
System.out.println("<"+line+">");
} catch (..)
{
...
}
So a BufferedReader is considered ready simply if the underlying stream is also ready. Since BufferedReader is a wrapper, this underlying stream could be any Reader implementation; hence the semantics of
ready()
are those declared on the interface:Returns true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.So you only really get timing guarantees, i.e. that
read()
will not block. The result of calling ready()
tells you absolutely nothing about the content you'll get back from a read()
call, and so cannot be used to elide a null check.
No comments:
Post a Comment