Java ProcessBuilder:关闭RedirectOutput文件的好方法(Java 8)
我启动过程并在我的应用程序中杀死他。
ProcessBuilder pb = new ProcessBuilder()
.directory(dir)
.command(command)
.redirectErrorStream(true)
.redirectOutput(file);
并用
void kill(Process p) {
Process kill = new ProcessBuilder("bash", "-c", "pkill -TERM -P " + pid).redirectErrorStream(true).start();
kill.waitFor();
if (p != null) {
if (p.isAlive()) {
p.destroy();
}
}
try {
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
} catch (IOException e) {
Util.errorLogger.error(e, "can't close log");
e.printStackTrace();
}
}
如何正确关闭文件的流杀死,以便不会有.fuse_hidden00123bd3nnnnnnnn文件?
PS Pkill
因为我不能杀死bash -c
儿童否则
可以使用P.SS,只能使用Java 8
I launch process and kill him in my app.
ProcessBuilder pb = new ProcessBuilder()
.directory(dir)
.command(command)
.redirectErrorStream(true)
.redirectOutput(file);
and killing with
void kill(Process p) {
Process kill = new ProcessBuilder("bash", "-c", "pkill -TERM -P " + pid).redirectErrorStream(true).start();
kill.waitFor();
if (p != null) {
if (p.isAlive()) {
p.destroy();
}
}
try {
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
} catch (IOException e) {
Util.errorLogger.error(e, "can't close log");
e.printStackTrace();
}
}
How can I properly close file's streams so that there won't be .fuse_hidden00123bd3nnnnn file?
P.s. pkill
because I can't kill bash -c
children otherwise
P.s.s. Only Java 8 can be used
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以迭代所有子进程以杀死:
请注意,一旦完成了一旦完成的
Waitfor()
,最好留给启动子过程的代码。You could iterate all child process to be killed with:
Note that IO stream cleanup is best left to the code that started the sub-process once the corresponding
waitFor()
was completed.