如果满足条件,我该如何运行for
?而不是使用scala.util.control.Breaks.break
,是否可以测试for
内的条件?
for(line <- source.getLines) {
if (line.equals("")) scala.util.control.Breaks.break
Console print "Message> "
dataWriter.write(line, InstanceHandle_t.HANDLE_NIL)
}
} catch {
case e: IOException =>{
答案 0 :(得分:8)
尝试takeWhile
for(line <- source.getLines.takeWhile(!_.isEmpty)) {
Console print "Message> "
dataWriter.write(line, InstanceHandle_t.HANDLE_NIL)
}
或
source.getLines.takeWhile(!_.isEmpty).foreach {
line =>
Console print "Message> "
dataWriter.write(line, InstanceHandle_t.HANDLE_NIL)
}