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:

import os._

@main def main() =
  // Specify the file path
  val filePath = os.pwd / "data.txt"

  // Read all lines from the file
  val lines = os.read.lines(filePath)

  // Iterate through each line and output it
  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:

10
20
30
40

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

import os._

@main def main() =
  // Specify the file path
  val filePath = os.pwd / "numbers.txt"
  
  // Read all lines from the file
  val lines = os.read.lines(filePath)

  // Initialize a variable to hold the sum of integers from the file
  var totalSum = 0

  // Iterate through each line, convert to integer, and add to totalSum
  lines.foreach { line =>
    try {
      val number = line.toInt
      totalSum += number // Add the integer to totalSum
    } catch {
      case _: NumberFormatException => 
        println(s"Invalid number format in line: $line")
    }
  }
  
  // Display the total sum
  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:

The 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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal