本文介绍了如何将Process Builder的输出重定向到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码启动进程构建器。我想知道如何将其输出重定向到String。
I am using the following code to start a process builder.I want to know how can I redirect its output to a String.
ProcessBuilder pb = new ProcessBuilder(System.getProperty("user.dir")+"/src/generate_list.sh", filename);
Process p = pb.start();
我尝试使用 ByteArrayOutputStream
但它没有'似乎工作。
I tried using ByteArrayOutputStream
but it didn't seem to work.
推荐答案
从 InputStream
中读取。您可以将输出附加到 StringBuilder
:
Read from the InputStream
. You can append the output to a StringBuilder
:
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
这篇关于如何将Process Builder的输出重定向到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!