Skip to content

Instantly share code, notes, and snippets.

@scalactic
Created November 30, 2021 17:34
Show Gist options
  • Save scalactic/07418e3a935d4faaeef805682466ab49 to your computer and use it in GitHub Desktop.
Save scalactic/07418e3a935d4faaeef805682466ab49 to your computer and use it in GitHub Desktop.
def withOpen(fileName: String, mode: String, encoding: String)(fp: Iterator[String] => Unit ): Unit = {
import scala.io.Source
val source = Source.fromFile(fileName)
val lines = source.getLines()
fp(lines)
source.close
}
withOpen("myfile.txt", "r", "utf-8") { lines =>
lines.foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment