流程需要重定向输入 [英] Process requires redirected input
本文介绍了流程需要重定向输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UNIX本机可执行文件,需要像这样输入参数
I have a UNIX native executable that requires the arguments to be fed in like this
prog.exe< foo.txt。
prog.exe < foo.txt.
foo.txt有两行:
bar
baz
foo.txt has two lines: bar baz
我正在使用java.lang.ProcessBuilder来执行此命令。不幸的是,prog.exe只能使用文件中的重定向。有什么方法可以模仿Java中的这种行为吗?
I am using java.lang.ProcessBuilder to execute this command. Unfortunately, prog.exe will only work using the redirect from a file. Is there some way I can mimic this behavior in Java?
当然,
ProcessBuilder pb = new ProcessBuilder("prog.exe", "bar", "baz");
不起作用。
谢谢!
推荐答案
ProcessBuilder pb = new ProcessBuilder("prog.exe");
Process p = pb.start();
OutputStream pos = p.getOutputStream();
InputStream fis = new FileInputStream("file.txt");
byte[] buffer = new byte[1024];
int read = 0;
while((read = fis.read(buffer)) != -1) {
pos.write(buffer, 0, read);
}
fis.close();
未经测试,但这样的事情应该有效。
Not tested, but something like this should work.
这篇关于流程需要重定向输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文