2014-11-04 21 views
0

不能寫入文件從我的Java代碼中,我有一個外部程序的調用,我使用以下命令:輸出在java中

int returnCodeC = 0; 
String cmnd = "dia -fa -fn res" + file1; 
final Process processCC = Runtime.getRuntime().exec(cmnd); 
BufferedReader bufC = new BufferedReader(new InputStreamReader(processCC.getInputStream())); 
returnCodeC = processCC.waitFor(); 
    } catch (Exception e1) {e1.printStackTrace();}} 

從呼叫輸出到外部程序直徑應該寫入名爲file1的文件 直到今天這一直正常運行。我想問題是,當dia的輸出很大時,我沒有輸出。是否有BufferReader的大小限制?

+0

你確定你還在讀書,從直徑什麼?我的意思是,它可能不是緩衝區限制。 – 2014-11-04 16:50:37

+0

當我從終端運行dia命令..一切運作良好..即使當我從java調用它它曾經工作(以防dia使用的輸入文件是小...因此輸出尺寸很小)現在,當從具有大輸入文件的java運行dia時,輸出(很大)不會寫入輸出文件。 – SLA 2014-11-04 16:55:56

+0

我不確定它是否有緩衝區限制問題。我仍然不知道問題所在。 – SLA 2014-11-04 16:57:38

回答

0

現在,我認爲我更好地理解問題,我會建議你使用processBuilder。它允許您對過程輸出進行卓越的控制。這裏是一個鏈接,可以幫助你:ProcessBuilder

的proccess接收一個字符串數組作爲參數,所以使用String [] cmd = {"dia", "-fa", "-fn", "res"};

+0

Runtime.getRuntime()。exec(cmnd);對一個String很滿意。也許這種用法已經過時了,但我懷疑這是造成問題的原因。 – 2014-11-04 18:33:14

+0

@SLA,我有點困惑:這是否(只有這個)真的解決了你的問題?你可以發佈運行代碼嗎?謝謝。 – 2014-11-05 23:04:05

0

這是更多的評論,但波普爾答案,但我的壞名聲不允許我發表評論。

如果stdout或stderr溢出其相應的緩衝區大小,您的進程將被阻止。

在shell中嘗試命令以查看會發生什麼,並啓動線程以讀取stdterr和stdout。

編輯:這是我在網上找到的,當我不需要stdout和stderr的內容,但只有命令運行時,它適用於我。 我在processCC.waitFor()之前調用dropOutput。

private int dropOutput(final Process p) { 

    final Thread t = new Thread("drop") { 

     @Override 
     public void run() { 
     _dropOutput(p); 
     }; 
    }; 

    t.start(); 
    int ev = 0; 
    Timer timer = null; 
    try { 
     timer = new Timer(true); 
     final InterruptTimerTask interrupter = new InterruptTimerTask(Thread.currentThread()); 
     timer.schedule(interrupter, 2 /*seconds*/* 1000 /*milliseconds per second*/); 
     if (p.waitFor() != 0) { 
     ev = p.exitValue(); 

     } 
    } catch (final InterruptedException e) { 
     // e.printStackTrace(); 
    } finally { 
     timer.cancel(); // If the process returns within the timeout period, we have to stop the interrupter 
         // so that it does not unexpectedly interrupt some other code later. 

     Thread.interrupted(); // We need to clear the interrupt flag on the current thread just in case 
          // interrupter executed after waitFor had already returned but before timer.cancel 
          // took effect. 
          // 
          // Oh, and there's also Sun bug 6420270 to worry about here. 
    } 
    return ev; 
    } 

    /** 
    * Just a simple TimerTask that interrupts the specified thread when run. 
    */ 
    class InterruptTimerTask extends TimerTask { 

    private final Thread thread; 

    public InterruptTimerTask(final Thread t) { 
     this.thread = t; 
    } 

    @Override 
    public void run() { 
     thread.interrupt(); 
    } 

    } 

    private void _dropOutput(final Process p) { 
    final InputStream istrm = p.getInputStream(); 
    // final InputStream istrm = p.getErrorStream(); 
    final InputStreamReader istrmrdr = new InputStreamReader(istrm); 
    final BufferedReader buffrdr = new BufferedReader(istrmrdr); 

    @SuppressWarnings("unused") 
    String data; 
    try { 
     while ((data = buffrdr.readLine()) != null) { 
     // System.out.println(data); 
     } 
    } catch (final IOException e) { 
     // do nothing 
    } 
    } 
+0

它從shell中運行..你可以閱讀我的評論嗎?這怎麼能解決 – SLA 2014-11-04 16:59:40

+0

幸運的是,我至少被允許評論我的答案。如果從shell運行命令,輸出有多大? – 2014-11-04 17:07:18