共計 587 個字符,預(yù)計需要花費(fèi) 2 分鐘才能閱讀完成。
在 Java 中,可以使用 Runtime
類的 exec()
方法執(zhí)行批處理文件。
以下是一個簡單的示例:
public class CallBatchFile {public static void main(String[] args) {try {// 調(diào)用批處理文件
Process process = Runtime.getRuntime().exec("path_to_batch_file.bat");
// 獲取批處理文件的輸出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {System.out.println(line);
}
// 等待批處理文件執(zhí)行完畢
int exitCode = process.waitFor();
System.out.println("Exit Code: " + exitCode);
} catch (IOException | InterruptedException e) {e.printStackTrace();
}
}
}
請將 path_to_batch_file.bat
替換為實(shí)際的批處理文件路徑。運(yùn)行以上代碼將調(diào)用批處理文件并打印出其輸出。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完