Lesson 2
Reading Files Line-by-Line in Scala
Introduction and Context Setting

Welcome to another lesson where we'll explore the fundamental technique of reading files line-by-line using Scala's os-lib library. In many real-world applications, especially those dealing with large files such as logs or continuous data streams, processing data line-by-line is vital for effective data management. By the end of this lesson, you'll be well-equipped to read and process file data line-by-line using Scala.

Reading Files Line-by-Line

To read a file line-by-line in Scala, you can utilize the os.read.lines method to obtain a collection of lines and then iterate over each line using the foreach method. Let's examine this process in action:

Scala
1import os._ 2 3@main def main() = 4 // Specify the file path 5 val filePath = os.pwd / "data.txt" 6 7 // Read all lines from the file 8 val lines = os.read.lines(filePath) 9 10 // Iterate through each line and output it 11 lines.foreach(println)

In this example, os.read.lines(filePath) reads all the lines from data.txt and returns them as a collection. We then print each line to the console using the foreach method.

Reading Integers from a File and Finding Their Sum

Imagine needing to process a set of numbers stored in a file — reading each integer and calculating their sum. This task can be easily accomplished using Scala’s os-lib.

Assume numbers.txt contains:

Plain text
110 220 330 440

Here's some Scala code to read integers from this file and calculate their sum:

Scala
1import os._ 2 3@main def main() = 4 // Specify the file path 5 val filePath = os.pwd / "numbers.txt" 6 7 // Read all lines from the file 8 val lines = os.read.lines(filePath) 9 10 // Initialize a variable to hold the sum of integers from the file 11 var totalSum = 0 12 13 // Iterate through each line, convert to integer, and add to totalSum 14 lines.foreach { line => 15 try { 16 val number = line.toInt 17 totalSum += number // Add the integer to totalSum 18 } catch { 19 case _: NumberFormatException => 20 println(s"Invalid number format in line: $line") 21 } 22 } 23 24 // Display the total sum 25 println(s"The sum of the numbers is: $totalSum")
  • Reading Numbers: Each line is parsed to an integer using toInt, which converts strings to integers.
  • Calculating Sum: The integers are summed in the totalSum variable.

Running this code will display the total sum of the numbers:

Plain text
1The sum of the numbers is: 100
Summary and Practice Preparation

In this lesson, you've acquired the skills to read a text file line-by-line using Scala — a crucial technique for processing large datasets efficiently. You've learned how to manage file I/O operations with os-lib, and how to handle each line of text data effectively in Scala applications. You're now well-prepared to tackle practice exercises applying these concepts and to further explore parsing techniques in future lessons. Continue building on these skills and enjoy the Scala journey!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.