例如:
读 InputStream 标准输出流,输出流东西很多
发现正常读取的时候 process 已经退出了
process.waitFor()方法 等待会导致缓冲区被填满,然后阻塞
思路是在 waitFor()前,异步地去读取输出流中的内容
但是不生效
简单代码 我写一块儿了 缩进比较恶心 主要是有什么好的解决思路吗?
Process process;
try {
process = runtime.exec(commands);
ThreadPools.LOG_COLLECT.execute(()->{
try (InputStream in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
String line;
File destfile = new File(...);
while ((line = br.readLine()) != null) {
FileUtils.writeStringToFile(destfile, line + "\n", StandardCharsets.UTF_8, true);
}
} catch (IOException e) {
//exception handler
}
});
process.waitFor();
} catch (IOException | InterruptedException e) {
//exception handler
}