簡體   English   中英

如何暫停運行.bat文件並在java中繼續

[英]How to run a .bat file with pause and proceed in java

我有一個處理多個 csv 文件的實用程序。 實際場景是:

  1. 運行 .bat 文件。
  2. 處理 file1.csv 時,.bat 文件會暫停並向我們顯示驗證輸出的選項,然后按 Enter 繼續處理 file2.csv。
  3. 同樣明智的是它適用於多個文件。

現在我需要在暫停和繼續的情況下自動執行這個執行 .bat 文件的過程。 目前,我可以通過以下方式執行 .bat 文件:

String cmd = "cmd /c run.bat");
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();

請幫助暫停和繼續選項。

編輯:在暫停時,我調用一個子線程來驗證輸出(當前正在自動驗證輸出)。 假設輸出為 TRUE,那么我需要繼續處理 .bat 文件,否則終止。

這是根據您的需要的簡短 POC。 你可以修改它。

.bat 文件

 @echo off @echo hi @echo File 1 underReview @echo Press 0 to continue PAUSE @echo Done Review.. PAUSE

Java 文件批量自動輸入

public static void main(String[] args) throws IOException, InterruptedException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "BATInput.bat");
    File dir = new File("C:\\BatTest");
    pb.directory(dir);
    Process p = pb.start();
    InputStream out = p.getInputStream();   
    OutputStream outConsole=p.getOutputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(out));
        //BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(outConsole)); 
        PrintWriter bout = new PrintWriter( p.getOutputStream() );
        String line = null;

        while ((line = in.readLine()) != null) {
            if (line.trim().length() > 0) {
                System.out.println(line);
                if(line.contains("Press 0"))
                {
                    int i=Integer.parseInt(br.readLine());
                    bout.write(i);
                    bout.write("\n");
                    bout.flush();
                }
            }
        }

        System.out.println("Executed Sucessfully");


}

輸出 Java 控制台

 hi File 1 underReview Press 0 to continue 0 Press any key to continue . . . Done Review.. Press any key to continue . . . Executed Sucessfully

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM