Runtime.getRuntime().exec()方法

Clarity-Black 2009-07-25 08:28:34
调用Runtime.getRuntime().exec("cmd /c start")可以弹出一个dos窗口,并且该方法返回一个Process对象,该类提供了获得输入、输出流的方法getInputStream(),和getOutputStream()方法;但是我下面这样为什么不能将弹出窗口中的输入,输出到控制台呢?各位大侠帮帮忙啊~

InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader output = new BufferedReader(is);
String outline;
while ((outline = output.readLine()) != null) {
System.out.println(outline);
}
...全文
154 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Clarity-Black 2009-07-25
  • 打赏
  • 举报
回复
那个command 中间要加一个start才能弹出dos窗口啊,而且这样也不能将dos里的输入读出来写到控制台
feishare 2009-07-25
  • 打赏
  • 举报
回复

public class TestRuntime {

public static void main(String[] args) throws IOException {
String filename = "D:\\Program\\TestCode\\out.txt";
String command = "cmd /C dir";

Runtime r = Runtime.getRuntime();
Process p = r.exec(command);

InputStreamReader is = new InputStreamReader(p.getInputStream());
BufferedReader output = new BufferedReader(is);
String outline;
while ((outline = output.readLine()) != null) {
System.out.println(outline);
}
}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧