On this question
Read entire file in Scala?
there is a comment to the first answer that reads
but I'd hate for people not to know they can do "io.File("/etc/passwd").slurp" in trunk.
When I try to do that, scala tells me
error: object File is not a member of package io
I have scala 2.9.1-1. Am I doing something wrong?
In a pipeline, the input file might be small but slow. If your program is slurping that means it's not working with the data as it becomes available and rather makes you wait for however long it might take for the input to complete.
File isn't part of the stdlib anymore. Instead you should use scala.io.Source
. To read an entire file you can do
val fileContents = io.Source.fromFile("my_file.txt").mkString
this should be avoided for large files though. In case of large files use Source.getLines
instead and process the file line by line. Source
also has a lot of other handy methods, so check them here http://www.scala-lang.org/api/current/index.html#scala.io.Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With