title: Does BufferedReader.ready() method ensure that readLine() method does not return NULL?
body: <p>I have such code to read a text file using <code>BufferedReader</code>:</p>
  
  <pre><code>BufferedReader reader=null;
      try {
          reader = new BufferedReader(new FileReader("file1.txt"));
  
          while (reader.ready()) {
              final String line = reader.readLine();
              System.out.println("&lt;"+line+"&gt;");
          } catch (..)
      {
          ...
      }
  </code></pre>
  
  <p>It works correctly but Findbugs reports a warning: </p>
  
  <blockquote>
    <p>NP_DEREFERENCE_OF_READLINE_VALUE : The
    result of invoking readLine() is
    dereferenced without checking to see
    if the result is null. If there are no
    more lines of text to read, readLine()
    will return null and dereferencing
    that will generate a null pointer
    exception.</p>
  </blockquote>
  
  <p>When I change <code>FileReader</code> to <code>StringReader</code>, i.e.</p>
  
  <pre><code>BufferedReader reader=null;
      try {
          reader = new BufferedReader(new StringReader("ABCD"));
  
          while (reader.ready()) {
              final String line = reader.readLine();
              System.out.println("&lt;"+line+"&gt;");
          } catch (..)
      {
          ...
      }
  </code></pre>
  
  <p>the <code>readLine</code> method returns <code>null</code> while the <code>ready</code> method always returns <code>true</code> - indeed this is an infinite loop.</p>
  
  <p>This seems that the <code>readLine</code> may return <code>null</code> even if <code>ready</code> returns <code>true</code>. But why does the behavior differ for different <code>Reader</code>s?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I do know the normal way to read a text file (just like Peter and Ali illustrated). but I read that piece of code from my colleague and realized that I don't know the <code>ready</code> method. Then I read the JavaDoc, but don't understand <code>block</code>. Then I did a test and posted this question. So, the better way to put this question might be:</p>
  
  <p>When will the input be blocking? How to use the <code>ready</code> method (or why not to use it)? Why do those 2 <code>Reader</code>s (<code>FileReader</code> and <code>StringReader</code>) behave differently with regards to the <code>ready</code> method?</p>