我想在我的 Java 程序中读取 c 应用程序的输出流。iremoted(可在此处获取:Link https://web.archive.org/web/20200505051207/http://www.osxbook.com/software/iremoted/download/iremoted.c)是一个 C 应用程序,如果按下 Apple Remote 上的按钮,它会输出单独的行,例如“0x19 Pressed”。如果我启动 iremoted 程序,一切都运行良好,每次按下按钮时,这些单独的行都会显示在我的屏幕上。
现在我想读取 Java 应用程序中 c 应用程序的输出流,以处理 Java 项目中 Apple Remote 的输入。
不幸的是我不知道为什么无法识别输入?
我用一个简单的 HelloWorld.c 程序进行了尝试,我的程序在本例中按预期响应(打印出 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
已缓冲,如果您不写入屏幕,它不会自动刷新。添加:
fflush(stdout);
after:
printf("%#lx %s\n", (UInt32)event.elementCookie,
(event.value == 0) ? "depressed" : "pressed");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)