java 调用命令执行操作方法
/** * 运行系统命令并返回命令结果 * @param command 要执行的命令 * @return 命令执行的结果 */ public static String executeCommand(String command) throws IOException { StringBuilder output = new StringBuilder(); // 启动命令 Process process = Runtime.getRuntime().exec(command); // 读取命令的输出 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line).append("\n"); // System.out.println(line); } // 读取尝试命令的任何错误 BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); while ((line = error.readLine()) != null) { output.append("错误: ").append(line).append("\n"); //System.err.println(line); } return output.toString(); }