Home »
Scala language
How to read entire file in Scala?
In this tutorial, we will learn how to open a file and read all its contents in Scala?
Submitted by Shivang Yadav, on January 03, 2021
Reading contents of a file is a part of file handling in Scala and is used to read data.
You can access the data of a file and use it in your program.
Here is the code used to read entire file in Scala,
import scala.io.Source
object MyClass {
def main(args: Array[String]) {
val filename = "fileopen.scala"
for (line <- Source.fromFile(filename).getLines) {
println(line)
}
}
}
Output:
Contents of file...