Quantcast
Channel: Read entire file in Scala? - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by comonad for Read entire file in Scala?

$
0
0

You do not need to parse every single line and then concatenate them again...

Source.fromFile(path)(Codec.UTF8).mkString

I prefer to use this:

import scala.io.{BufferedSource, Codec, Source}import scala.util.Trydef readFileUtf8(path: String): Try[String] = Try {  val source: BufferedSource = Source.fromFile(path)(Codec.UTF8)  val content = source.mkString  source.close()  content}

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>