title: Does a Java InputStream help or hurt memory usage with large files?
body: <p>I see some posts on StackOverflow that contradict each other, and I would like to get a definite answer. </p>
  
  <p>I started with the assumption that using a Java InputStream would allow me to stream bytes out of a file, and thus save on memory, as I would not have to consume the whole file at once. And that is exactly what I read here:</p>
  
  <blockquote>
    <p>Loading all bytes to memory is not a good practice. Consider returning the file and opening an input stream to read it, so your application won't crash when handling large files. – andrucz </p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/37255963/download-file-to-stream-instead-of-file">Download file to stream instead of File</a></p>
  
  <p>But then I used an InputStream to read a very large Microsoft Excel file (using the Apache POI library) and I ran into this error: </p>
  
  <p><a href="https://stackoverflow.com/questions/11118703/java-lang-outofmemory-exception-while-reading-excel-file-xlsx-using-poi">java.lang.outofmemory exception while reading excel file (xlsx) using POI</a></p>
  
  <p>I got an OutOfMemory error. </p>
  
  <p>And this crucial bit of advice saved me:</p>
  
  <blockquote>
    <p>One thing that'll make a small difference is when opening the file to start with. If you have a file, then pass that in! Using an InputStream requires buffering of everything into memory, which eats up space. Since you don't need to do that buffering, don't!</p>
  </blockquote>
  
  <p>I got rid of the InputStream and just used a bare java.io.File, and then the OutOfMemory error went away.</p>
  
  <p>So using java.io.File is better than an InputSteam, when it comes to memory use? That doesn't make any sense.</p>
  
  <p>What is the real answer?</p>