ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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...
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.