从另一个进程的输出流中读取
我想在我的Java程序中读取c-Application的输出流。 iremoted (此处提供: http://osxbook.com/software /iremoted/download/iremoted.c )是一个C-Application,如果按下Apple Remote遥控器上的按钮,它会输出单独的行,例如0x19被按下。如果我启动iremoted程序,一切都很顺利,每次按下按钮,这些单独的行都显示在我的屏幕上。
现在我想在我的Java应用程序中读取c-application的输出流来处理Java项目中Apple Remote的输入。
不幸的是我不知道为什么没有输入被重新定位?
i wanted to read the output-stream of a c-Application in my Java program. iremoted (available here: http://osxbook.com/software/iremoted/download/iremoted.c) is a C-Application that puts out seperate lines like "0x19 pressed" if a button on my Apple Remote is pressed. If i start the iremoted program everything is doing well and these separate lines are shown on my screen everytime I pressed a button. Now I wanted to read the output-stream of the c-application in my Java application to process inputs of the Apple Remote in Java projects. Unfortunately i don't know why no input is regocnized?
我尝试了一个简单的HelloWorld.c程序和我的程序在这种情况下按预期响应(打印出HelloWorld)。
I tried it with a simple HelloWorld.c program and my program responded as intended in this case (prints out HelloWorld).
为什么不能用iremoted程序工作?
public class RemoteListener {
public void listen(String command) throws IOException {
String line;
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
} catch (Exception e) {
System.err.println("Could not execute program. Shut down now.");
System.exit(-1);
}
Reader inStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader in = new BufferedReader(inStreamReader);
System.out.println("Stream started");
while((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
System.out.println("Stream Closed");
}
public static void main(String args[]) {
RemoteListener r = new RemoteListener();
try {
r.listen("./iremoted"); /* not working... why?*/
// r.listen("./HelloWorld"); /* working fine */
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
stdout
是缓冲的,如果你没有写入屏幕,它不会自动刷新。添加:
stdout
is buffered and it's not automatically flushed if you are not writing to screen. Add:
fflush(stdout);
之后:
printf("%#lx %s\n", (UInt32)event.elementCookie,
(event.value == 0) ? "depressed" : "pressed");