scala read lines from file

To read lines from a file in Scala, you can use the class. Here's an example:


val filename = "path/to/your/file.txt"
val lines = Source.fromFile(filename).getLines()

for (line <- lines) {

In this example, we first import the class. Then, we specify the filename of the file we want to read from. Next, we use the fromFile method of Source to create a BufferedSource object. We can then call the getLines method on the BufferedSource object to obtain an iterator over the lines of the file.

Finally, we iterate over the lines using a for loop and print each line.

Replace "path/to/your/file.txt" with the actual path to your file. Make sure to handle any potential exceptions that may occur when working with files, such as FileNotFoundException or IOException.