2015-10-27 68 views
1

我正在編寫一個使用第三方數學軟件「Maxima」的程序。該程序是一個命令行界面,因此它可以通過我的Java程序與簡單的I/O路由進行通信。我已經想出瞭如何從Java內部運行該程序,並且我已經閱讀了很多關於如何重新配置​​System.out以及InputStreams/OutputStreams如何工作的內容,但我無法弄清楚如何執行以下操作認爲應該是一個非常簡單的任務):以編程方式與Java中的I/O程序進行交互

  1. 輸出到千里馬從Java命令,(如字符串「5 + 5;」)
  2. 檢索千里馬的輸出,並從Java代碼處理它(如可能會打印給定的字符串+「blah」)。
  3. 輸出另一個命令千里馬從Java ...

- 下面是代碼,將運行千里馬,讓我與它在Eclipse控制檯

public static void main(final String[] args) { 

    // An idea I had for manipulaing how the printstream works. 
    // Set the system.out to be a custom Prinstream. 
    // final PrintStream interceptor = new Interceptor(origOut); 
    // System.setOut(interceptor); 

    // Run the program: 
    final String programLocation = "\"C:\\Program Files (x86)\\Maxima-sbcl-5.37.2\\bin\\maxima.bat\""; 
    final ProcessBuilder pb = new ProcessBuilder(); 
    pb.redirectInput(Redirect.INHERIT); // Inherit I/O 
    pb.redirectOutput(Redirect.INHERIT); 
    pb.command(programLocation); 

    try { 
     // Start the program and allow it to run in Eclipse's/the program's 
     // console. 
     pb.start().waitFor(); 
    } catch (final InterruptedException e) { 
     e.printStackTrace(); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 

} 
互動

這允許以下風格的互動:Image of Console Interaction

+3

你應該*不*被重定向輸入和輸出。缺省是能夠獲得進程的輸出,輸入和錯誤流,並能夠寫入和讀取它們 - 通過重定向您將失去該能力。 – RealSkeptic

+0

@RealSkeptic,所以我應該使用像'BufferedReader中R =新的BufferedReader(新的InputStreamReader( \t \t \t \t process.getInputStream()));'創建一個工具,它可以 「讀出」 千里馬的輸出? –

+0

簡而言之,是的。 – RealSkeptic

回答

0

感謝來自@RealSkeptic的智慧話語,我認爲我工作ou這裏有一個解決方案。

關鍵是構建一個BufferedWriter和一個BufferedReader來與Maxima的I/O進行交互。那就是:

BufferedWriter w = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream())); 

這兩行代碼創建緩衝讀寫器,它可以將數據輸入Maxima,並讀取Maxima輸出。這裏是一個(相當長)的情況下使用這種方法,我用做基本上是我問的問題:

public class TestClass { 

public static void main(final String[] args) { 
    @SuppressWarnings("unused") 
    final TestClass ts = new TestClass(); 
} 

private BufferedWriter w; 
private BufferedReader r; 

public TestClass() { 
    // Start the process using process builder 
    final String programLocation = "\"C:\\Program Files (x86)\\Maxima-sbcl-5.37.2\\bin\\maxima.bat\""; 
    final ProcessBuilder pb = new ProcessBuilder(); 
    pb.command(programLocation); 
    Process process; 
    try { 
     process = pb.start(); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
     process = null; 
     // killProgram(); 
    } 

    // Build your own wrappers for communicating with the program. 
    w = new BufferedWriter(
      new OutputStreamWriter(process.getOutputStream())); 
    r = new BufferedReader(new InputStreamReader(process.getInputStream())); 

    // Print the five starting messages. 
    printFromBuffer(); 
    printFromBuffer(); 
    printFromBuffer(); 
    printFromBuffer(); 
    printFromBuffer(); 

    // Run the following three commands in Maxima 
    runCommand("5+5;"); 
    runCommand("2*65;"); 
    runCommand("quit();"); 
} 

/** 
* Runs the given string and prints out the returned answer. 
*/ 
private void runCommand(final String s) { 
    try { 
     w.write(s); 
     w.flush(); 
     printFromBuffer(); 
     printFromBuffer(); 
    } catch (final IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void printFromBuffer() { 
    try { 
     final String s = r.readLine(); 

     System.out.println(s + " -blah"); 

    } catch (final IOException e) { 
     System.err.println(e.getMessage()); 
     e.printStackTrace(); 
    } 
} 
} 
相關問題