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,


object MyClass {
    def main(args: Array[String]) {
        val filename = "fileopen.scala"
        for (line <- Source.fromFile(filename).getLines) {


Contents of file...

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.