process.text的错误等价物?

rip*_*234 6 groovy

您可以使用.text 获取整个输出流:

def process = "ls -l".execute()
println "Found text ${process.text}"
Run Code Online (Sandbox Code Playgroud)

是否有一个简洁的等价物来获取错误流?

tim*_*tes 7

您可以使用waitForProcessOutput哪两个Appendables(这里的文档)

def process = "ls -l".execute()
def (output, error) = new StringWriter().with { o -> // For the output
  new StringWriter().with { e ->                     // For the error stream
    process.waitForProcessOutput( o, e )
    [ o, e ]*.toString()                             // Return them both
  }
}
// And print them out...
println "OUT: $output"
println "ERR: $error"
Run Code Online (Sandbox Code Playgroud)

  • 主要是因为使用.text成员是危险的.如果输出到输出或错误流的文本超出了buffersize,那么您的进程将暂停,直到读取某些流.当您不知道使用单独的线程捕获流时输出将持续多长时间(通常是错误的情况)时,这实际上是一个好主意. (2认同)