2017-03-01 105 views
1

我試圖在Java程序中運行split命令。當我在控制檯參數--verbose運行它,它打印所生成的塊如下:Java在運行時輸出打印輸出

creating file 'chunk00' 
creating file 'chunk01' 
creating file 'chunk02' 

但是,當我在java程序運行它,這些輸出都將在完成處理後進行印刷。當split進程正在運行時,我必須做些什麼才能獲得輸出?

我用下面的代碼:

ProcessBuilder pb = new ProcessBuilder("split", "-a 2", "-d", "-b 52MB","--verbose",path+"/"+db,"chunk"); 
     pb.redirectErrorStream(true); 
     File workingFolder = new File("/home/hajibaba"); 
     pb.directory(workingFolder); 
     Process proc = pb.start(); 
     BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 

     // read the output from the command 
     String s = null; 
     while ((s = stdInput.readLine()) != null) 
     { 
      System.out.println(s); 
     } 
     proc.waitFor(); 

但是,它適用於使用echo打印相同的結果bash腳本。

+1

還必須拆分 「-a 2」 到 「-a」, 「2」,也是 「-b 52MB」 改爲 「-B」, 「52MB」 –

回答