Normally, they will tell you to
import scala.io.Source
for(line <- Source.fromPath("myfile.txt").getLines())
println(line)
which seems to leave the file open. What is a closeable counterpart?
Normally, they will tell you to
import scala.io.Source
for(line <- Source.fromPath("myfile.txt").getLines())
println(line)
which seems to leave the file open. What is a closeable counterpart?
You can close the Source
and this will close your file.
import scala.io.Source
val source = Source.fromFile("myfile.txt")
for (line <- source.getLines())
println(line)
source.close()
fromFile
. –
Tacmahack © 2022 - 2024 — McMap. All rights reserved.
fromPath is not a member of Source
and I must use fromFile instead. – Opinionated