Scala: How to read input from one file while writing output to another file alvin June 14, 2017 - 3:53pm

Without much introduction or discussion, here’s a Scala example that shows how to read from one text file while simultaneously writing the uppercase version of the text to a second output file:

How to open and read text files in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”

Back to top


You want to open a plain-text file in Scala and process the lines in that file.

Back to top


There are two primary ways to open and read a text file:

Table of Contents

  1. Problem
  2. Solution
Back to top
The ‘using’ control structure in Beginning Scala (by David Pollak) alvin February 11, 2013 - 10:38am

In David Pollak's book, Beginning Scala, he introduces a using control structure that is helpful in several ways. First, it can be used to automatically close any resource that has a close method. Second, it's a great example of how to create a custom control structure in Scala.


Here's the source code for the using control structure he created: