说我有这样的功能:检索的ProcessBuilder
public String runCommand(parameters, boolean interactive)
{
Process p = null;
// some code
try
{
final ProcessBuilder pb = new ProcessBuilder(my_command);
pb.inheritIO(); // So the output is displayed on the console
p = pb.start();
p.waitFor();
}
catch(IOException | InterruptedException e)
{
e.printStackTrace();
}
if (interactive)
{
return p.exitValue() + "";
}
else
{
// return the stdout of the process p
}
}
我想要做的就是回到我通过ProcessBuilder
运行的进程的标准输出,只有在interactive
布尔设置为false
。但是,我无法确定如何将标准输出重定向到ProcessBuilder
的变量。请不要使用inheritIO()
,所以当我使用像adb shell
这样的函数时,shell会显示在我的控制台中,这是我想要的行为。所以基本上,至于现在,我可以看到在控制台的标准输出,但我不知道如何在函数中返回它,所以我可以使用这个值作为未来的东西的变量。