如何使用输入参数(如“./flows.sh suspend”)执行shell脚本文件,并使用java中的java将结果打印到文件中?
答案 0 :(得分:2)
这是一个执行shell脚本并读取其输出的简单代码:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String[] command = { "./flows.sh", "suspend" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s); // Replace this line with the code to print the result to file
}
}
}
要将其打印到文件,只需将System.out.println替换为要写入文件的代码
答案 1 :(得分:-1)