开发者

How to make a Java class listening for events on the "stdout" of a C program

public static void main(String[] args) {

    try {
              String line;
              InputStream stdout = null;
              OutputStream stdin = null;

              Process process = Runtime.getRuntime().exec("test.exe");
              stdout = process.getInputStream ();
              stdin = process.getOutputStream ();

              line = "Hello World" + "\n";   
              stdin.write(line.getBytes() );
              stdin.flush();

              stdin.close();

              BufferedReader brCleanUp = 
              new BufferedReader (new InputStreamReader (stdout));
              while ((line = brCleanUp.readLine ()) != null) {
                  System.out.println ("[Stdout] " + line);
              }
                brCleanUp.close();
    }
    catch(Exception e){
        System.out.println("Error\n");
    }
}

T开发者_开发百科he code above allows a Java class to write in the stdin of "test.exe" (C program) and to read its stdout Now, how can I make a Java Class which listens for the events on the stdout of a C program. That is a Java event listener that will be called each time a new line is written in the stdout of the C program


You need to have a separate thread which blocks reading the input stream. It can then fire events on your main thread (for instance using java.awt.EventQueue.invokeLater for a Swing GUI).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜