我想读取输入并仅打印输出,如
(阅读1 2 3 4 5,打印1 2 3 4 5)使用shell执行java
当我运行后续代码输出时看起来像这样" echo 1 2 3 4 5 | java task4"。我的班级名称是task4。
我真的不知道该怎么做。有什么建议吗?
String command="echo 1 2 3 4 5 | java task4";
Process cmdProc = Runtime.getRuntime().exec(command);
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(cmdProc.getInputStream()));
String line;
while ((line = stdoutReader.readLine()) != null) {
// process procs standard output here
System.out.println(line);
}
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(cmdProc.getErrorStream()));
String line1;
while ((line1 = stderrReader.readLine()) != null) {
// process procs standard error here
}
int retValue = cmdProc.exitValue();