2013-10-08 58 views
1

我有一個編譯和運行其他Java程序的Java程序。我也有.txt文件,這些文件的輸入被輸入到其他Java程序中。從另一個獲得正在運行的Java程序的輸出

我想知道如何做的是捕獲輸入文件的運行輸出?

+2

[這](http://stackoverflow.com/questions/5389632/capturing-contents-of-standard-output-in-java)其他計算器問題可能是你在找什麼。 –

+0

@DavidWallace OP不知道該怎麼辦?因此在這裏發佈的問題。如果有些身體不知道那麼它沒用,問你試過什麼? – SpringLearner

+1

@javaBeginner - 幫助那些沒有嘗試爲自己編寫代碼的人不是Stack Overflow的作用。這個想法是在遇到困難時回答具體問題,而不是從完全空白的板岩開發代碼。檢查常見問題。 –

回答

3

我假設你通過任何的ProcessBuilder或的Runtime.exec(調用其他程序)都返回其具有方法Process對象getInputStream()和getErrorStream(),它們允許您監聽其他進程的輸出和錯誤(stdout,stderr)流。

考慮下面的代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

public class Test { 

    public static void main(String[] args){ 
     Test t = new Test(); 

     t.start(); 
    } 

    private void start(){ 
     String command = //Command to invoke the program 

     ProcessBuilder pb = new ProcessBuilder(command); 

     try{ 
      Process p = pb.start(); 

      InputStream stdout = p.getInputStream(); 
      InputStream stderr = p.getErrorStream(); 

      StreamListener stdoutReader = new StreamListener(stdout); 
      StreamListener stderrReader = new StreamListener(stderr); 

      Thread t_stdoutReader = new Thread(stdoutReader); 
      Thread t_stderrReader = new Thread(stderrReader); 

      t_stdoutReader.start(); 
      t_stderrReader.start(); 
     }catch(IOException n){ 
      System.err.println("I/O Exception: " + n.getLocalizedMessage()); 
     } 
    } 

    private class StreamListener implements Runnable{ 
     private BufferedReader Reader; 
     private boolean Run; 

     public StreamListener(InputStream s){ 
      Reader = new BufferedReader(new InputStreamReader(s)); 
      Run = true; 
     } 

     public void run(){ 
      String line; 

      try{ 
       while(Run && (line = Reader.readLine()) != null){ 
        //At this point, a line of the output from the external process has been grabbed. Process it however you want. 
        System.out.println("External Process: " + line); 
       } 
      }catch(IOException n){ 
       System.err.println("StreamListener I/O Exception!"); 
      } 
     } 
    } 
} 
0

把握這個例子:

// Try these charsets for encoding text file 
    String[] csStrs = {"UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16", "GB2312", "GBK", "BIG5"}; 
    String outFileExt = "-out.txt"; // Output filenames are "charset-out.txt" 

    // Write text file in the specified file encoding charset 
    for (int i = 0; i < csStrs.length; ++i) { 
    try (OutputStreamWriter out = 
      new OutputStreamWriter(
       new FileOutputStream(csStrs[i] + outFileExt), csStrs[i]); 
      BufferedWriter bufOut = new BufferedWriter(out)) { // Buffered for efficiency 
     System.out.println(out.getEncoding()); // Print file encoding charset 
     bufOut.write(message); 
     bufOut.flush(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 

    // Read raw bytes from various encoded files 
    // to check how the characters were encoded. 
    for (int i = 0; i < csStrs.length; ++i) { 
    try (BufferedInputStream in = new BufferedInputStream( // Buffered for efficiency 
      new FileInputStream(csStrs[i] + outFileExt))) { 
     System.out.printf("%10s", csStrs[i]); // Print file encoding charset 
     int inByte; 
     while ((inByte = in.read()) != -1) { 
      System.out.printf("%02X ", inByte); // Print Hex codes 
     } 
     System.out.println(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 

    // Read text file with character-stream specifying its encoding. 
    // The char will be translated from its file encoding charset to 
    // Java internal UCS-2. 
    for (int i = 0; i < csStrs.length; ++i) { 
    try (InputStreamReader in = 
      new InputStreamReader(
       new FileInputStream(csStrs[i] + outFileExt), csStrs[i]); 
      BufferedReader bufIn = new BufferedReader(in)) { // Buffered for efficiency 
     System.out.println(in.getEncoding()); // print file encoding charset 
     int inChar; 
     int count = 0; 
     while ((inChar = in.read()) != -1) { 
      ++count; 
      System.out.printf("[%d]'%c'(%04X) ", count, (char)inChar, inChar); 
     } 
    System.out.println(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    } 

}}

相關問題