20

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?

Community
  • 1
  • 1
Karel Bílek
  • 34,538
  • 28
  • 89
  • 139

1 Answers1

23

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

drexin
  • 23,909
  • 4
  • 64
  • 81