title: Read entire file in Scala?
body: <p>What's a simple and canonical way to read an entire file into memory in Scala?  (Ideally, with control over character encoding.)</p>
  <p>The best I can come up with is:</p>
  <pre><code>scala.io.Source.fromPath(&quot;file.txt&quot;).getLines.reduceLeft(_+_)
  </code></pre>
  <p>or am I supposed to use one of <a href="https://stackoverflow.com/questions/731365/reading-and-displaying-data-from-a-txt-file">Java's god-awful idioms</a>, the best of which (without using an external library) seems to be:</p>
  <pre><code>import java.util.Scanner
  import java.io.File
  new Scanner(new File(&quot;file.txt&quot;)).useDelimiter(&quot;\\Z&quot;).next()
  </code></pre>
  <p>From reading mailing list discussions, it's not clear to me that <code>scala.io.Source</code> is even supposed to be the canonical I/O library.  I don't understand what its intended purpose is, exactly.</p>
  <p>... I'd like something dead-simple and easy to remember.  For example, in these languages it's very hard to forget the idiom ...</p>
  <pre><code>Ruby    open(&quot;file.txt&quot;).read
  Ruby    File.read(&quot;file.txt&quot;)
  Python  open(&quot;file.txt&quot;).read()
  </code></pre>