title: Why doesn't Scala Source close the underlying InputStream?
body: <p>I'm using Scala <code>Source.fromFile</code> however I can't seem to find a nice way of getting it to <code>close</code> the underlying <code>InputStream</code> once the file has been read.</p>
  
  <p>Here's my code that will fail with an <code>AssertionError</code> because the file cannot be deleted.</p>
  
  <pre><code>  def main(args : Array[String]) : Unit = {
  
      val myFile = new File("c:/tmp/doodah.txt")
      var src = Source.fromFile(myFile)
      src.getLines.foreach(l =&gt; print(l))
  
      val deleted: Boolean = myFile.delete
  
      assert (deleted , "File was not deleted - maybe the stream hasn't been closed in Source")
  
    }
  </code></pre>
  
  <p>Source has a method called <code>reset</code> however all that this does is recreate the source from the file.</p>
  
  <p>Internally <code>Source</code> creates an underlying <code>BufferedSource</code> that has a <code>close</code> method. However this is not exposed from Source.</p>
  
  <p>I'd hope that Source would release the file handle once the contents of the file had been read but it doesn't seem to do that.</p>
  
  <p>The best workaround I've seen so far is to essentially cast the <code>Source</code> to a <code>BufferedSource</code> and call <code>close</code>.</p>
  
  <pre><code>try {
    src.getLines.foreach(l =&gt; print(l))
  }
  finally src match { case b: scala.io.BufferedSource =&gt; b.close }
  </code></pre>
  
  <p>Alternatively I could create a <code>Source</code> from an <code>InputStream</code> and manage the closing myself.</p>
  
  <p>However this seems somewhat <em>dirty</em>. How are you supposed to release the file handle when using <code>Source</code>?</p>