我正在尝试将进程的输出变为String,但我遇到了一些问题。不知何故,我设法将输出保存在一个单独的文件中(在stackoverflow.com上找到了一些东西)。以下是启动流程的代码:
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + File.separator + "bin" + File.separator
+ "java";
String classpath = System.getProperty("java.class.path");
String className = klasse.getCanonicalName();
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath,
className, ip, port, team, "okay");
文件中的写作是:
File log = new File("logfile_deleteable" + a);
builder.redirectOutput(Redirect.appendTo(log));
assert builder.redirectInput() == Redirect.PIPE;
assert builder.redirectOutput().file() == log;
assert process[a].getInputStream().read() == -1;
最后:
process[a] = builder.start();
答案 0 :(得分:0)
试试这个
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process[a].getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ( (line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(System.getProperty("line.separator"));
}
String output = stringBuilder.toString();