gpt4 book ai didi

scala - 如何关闭枚举文件?

转载 作者:行者123 更新时间:2023-12-01 11:36:05 25 4
gpt4 key购买 nike

比如说,在我的行动中:

val linesEnu = {
val is = new java.io.FileInputStream(path)
val isr = new java.io.InputStreamReader(is, "UTF-8")
val br = new java.io.BufferedReader(isr)
import scala.collection.JavaConversions._
val rows: scala.collection.Iterator[String] = br.lines.iterator
Enumerator.enumerate(rows)
}

Ok.feed(linesEnu).as(HTML)

如何关闭阅读器/流?

最佳答案

有一个 onDoneEnumerating 回调函数,其功能类似于 finally(无论 Enumerator 是否失败,都会被调用)。您可以在那里关闭流。

val linesEnu = {
val is = new java.io.FileInputStream(path)
val isr = new java.io.InputStreamReader(is, "UTF-8")
val br = new java.io.BufferedReader(isr)
import scala.collection.JavaConversions._
val rows: scala.collection.Iterator[String] = br.lines.iterator
Enumerator.enumerate(rows).onDoneEnumerating {
is.close()
// ... Anything else you want to execute when the Enumerator finishes.
}
}

关于scala - 如何关闭枚举文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com