我有以下Scala代码:
@Test def test2() = {
try {
val data_in = IOUtils.toString(getClass().getClassLoader()
.getResourceAsStream("com/myProject/myPackage/myInput.txt"))
val data_out = MyTool.process(data_in)
println(data_out)
} catch {
case e: Exception =>
println("process failed")
}
}
当myInput.txt只有一行时,代码运行良好。但是,我想知道当myInput.txt有多行时,如何将data_in
分解为多行,并使用MyTool.process()
处理每一行?
谢谢!
试着像这样使用scala.io.Source.fromInputStream
import scala.io.Source
@Test def test2() = {
try {
val data_in = getClass().getClassLoader()
.getResourceAsStream("com/myProject/myPackage/myInput.txt")
for (line <- Source.fromInputStream(data_in).getLines()) {
val data_out = MyTool.process(line)
println(data_out)
}
} catch {
case e: Exception =>
println("process failed")
}
}