我知道输入流在 Groovy 中这种块的末尾会自动关闭:
def exec = ""
System.in.withReader {
println "input: "
exec = it.readLine()
}
但是如果我想做这样的事情,有没有办法重新打开流:
def exec = ""
while(!exec.equals("q")) {
System.in.withReader {
println "input: "
exec = it.readLine()
}
if(!exec.equals("q")) {
//do something
}
}
当我尝试这个时,我在第二次执行 while 循环时收到此错误:
Exception in thread "main" java.io.IOException: Stream closed
那么实现这一目标的最佳方法是什么?
谢谢。