作者paulyanzi (消失)
站内java
标题[J2SE] 如何让exec() 可以继续输入/取得文字
时间Sun Nov 22 12:27:42 2009
想要去跑一个 xx.exe的档案
请问要怎麽让exec 持续的可以输入输出资讯呢?
以cmd的情况就是
1. C:\me.exe
2. sfjpoiawenoidsjv (要输入的文字)
3. 取得output的文字
然後 Step 2,3 一直轮流 直到没有东西输入
也许有人会问说1,2可以和在一起,但是这个exe这样和在一起下 就是没有用
简略程式码如下:
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("\"C:\\me.exe\"");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>1");
while ((line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>2");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.31.141
1F:→ legnaleurc:不是有 getOutputStream 和 getInputStream ? 11/22 18:19
2F:推 ogamenewbie:getErrorStream()? ERROR? 11/22 18:48