title: difference between input.read and input.read(array, offset, length)
body: <p>I'm trying to understand how inputstreams work. The following block of code is one of the many ways to read data from a text file:-</p>
  
  <pre><code>File file = new File("./src/test.txt");
  InputStream input = new BufferedInputStream (new FileInputStream(file));
  int data = 0;
  
  while (data != -1)   (-1 means we reached the end of the file)
   {
       data = input.read(); //if a character was read, it'll be turned to a bite and we get the integer representation of it so a is 97 b is 98
       System.out.println(data + (char)data); //this will print the numbers followed by space then the character
   }
  
  input.close();
  </code></pre>
  
  <p>Now to use <code>input.read(byte, offset, length)</code> i have this code. I got it from <a href="http://www.javapractices.com/topic/TopicAction.do?Id=245" rel="nofollow">here</a></p>
  
  <pre><code>File file = new File("./src/test.txt");
  InputStream input = new BufferedInputStream (new FileInputStream(file));
  int totalBytesRead = 0, bytesRemaining, bytesRead;
  byte[] result = new byte[ ( int ) file.length()];
  
  while ( totalBytesRead &lt; result.length )
   {
       bytesRemaining = result.length - totalBytesRead;
       bytesRead = input.read ( result, totalBytesRead, bytesRemaining );
       if ( bytesRead &gt; 0 )
        totalBytesRead = totalBytesRead + bytesRead;
  
       //printing integer version of bytes read
       for (int i = 0; i &lt; bytesRead; i++)
        System.out.print(result[i] + " ");
  
       System.out.println();
  
       //printing character version of bytes read
       for (int i = 0; i &lt; bytesRead; i++)
        System.out.print((char)result[i]);
   }
  
  input.close();
  </code></pre>
  
  <p>I'm assuming that based on the name <code>BYTESREAD</code>, this read method is returning the number of bytes read. In the documentation, it says that the function will try to read as many as possible. So there might be a reason why it wouldn't.</p>
  
  <p><strong>My first question is: What are these reasons?</strong></p>
  
  <p>I could replace that entire while loop with one line of code: <code>input.read(result, 0, result.length)</code></p>
  
  <p>I'm sure the creator of the article thought about this. It's not about the output because I get the same output in both cases. So there has to be a reason. At least one. What is it?</p>