我需要在命令行执行一个命令,并且在执行该命令后需要提供一系列是或否答案。 例如:
<小时/>>./somecommand(返回)
你确定吗? [是/否]:是(返回)
你真的确定这一点吗? [是/否]:是(返回)
最后的机会。 [是/否]:否(返回)
好的。
<小时/>我正在尝试使用以下代码片段来完成此任务。
try {
// Execute command
String command = "somecommand";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("yes".getBytes());
out.flush();
out = child.getOutputStream();
out.write( "yes".getBytes() );
out.flush();
out = child.getOutputStream();
out.write( "no".getBytes() );
out.close();
} catch (IOException e) {
}
如您所见,我尝试使用“out = child.getOutputStream()”三次,每次写入后刷新“out”。但这似乎不起作用。 知道如何做到这一点吗?
最佳答案
在 while 循环中尝试 java readLine()
命令。
给它一个终止条件,例如 Yes
或 No
。
关于java - 使用 exec 将多个输入发送到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7273832/