通常,他们会告诉你要
import scala.io.Source
for(line <- Source.fromPath("myfile.txt").getLines())
println(line)
这似乎是让文件保持打开状态。有没有可关闭的对应方法?
import scala.io.Source
for(line <- Source.fromPath("myfile.txt").getLines())
println(line)
这似乎是让文件保持打开状态。有没有可关闭的对应方法?
您可以关闭 源代码
这样会关闭您的文件。
import scala.io.Source
val source = Source.fromFile("myfile.txt")
for (line <- source.getLines())
println(line)
source.close()
fromPath is not a member of Source
,我必须使用 fromFile。 - Valentin TihomirovfromFile
。 - Łukasz