Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala file slurp

Tags:

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?

like image 654
Karel Bílek Avatar asked Jun 15 '12 08:06

Karel Bílek


People also ask

What does it mean to slurp a file?

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.


1 Answers

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

like image 147
drexin Avatar answered Oct 07 '22 21:10

drexin