如何将字符串输入发送到 ProcessBuilder
how to send stringed input into ProcessBuilder
class envir {
public void run() throws IOException {
ProcessBuilder builder = new ProcessBuilder("bash");
builder.redirectInput(ProcessBuilder.Redirect.PIPE);
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
builder.redirectErrorStream(true);
Process process = builder.start();
System.out.println(process.getInputStream());
}
}
如何才能发送一个字符串作为我的流程构建器的输入,以自动执行也使用线程的 cli(例如 env python3
)?
如果您需要更多信息,请询问;我不擅长措辞这些问题。
Process
的流名称令人困惑。你真正想要的是 output stream:
public abstract OutputStream getOutputStream()
Returns the output stream connected to the normal input of the subprocess.
所以:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
然后写入:
bw.write("Your string");
bw.newLine();
class envir {
public void run() throws IOException {
ProcessBuilder builder = new ProcessBuilder("bash");
builder.redirectInput(ProcessBuilder.Redirect.PIPE);
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
builder.redirectErrorStream(true);
Process process = builder.start();
System.out.println(process.getInputStream());
}
}
如何才能发送一个字符串作为我的流程构建器的输入,以自动执行也使用线程的 cli(例如 env python3
)?
如果您需要更多信息,请询问;我不擅长措辞这些问题。
Process
的流名称令人困惑。你真正想要的是 output stream:
public abstract OutputStream getOutputStream()
Returns the output stream connected to the normal input of the subprocess.
所以:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
然后写入:
bw.write("Your string");
bw.newLine();