【问题标题】:Set System.setOut(); to Command Prompt [duplicate]设置 System.setOut();到命令提示符[重复]
【发布时间】:2013-04-21 16:52:26
【问题描述】:

我想像这样将输出流设置为命令提示符:

Process p = Runtime.getRuntime()
              .exec("C:\\Windows\\System32\\cmd.exe /c start cls");
System.setOut(new PrintStream(p.getOutputStream()));

但它不起作用,为什么?

【问题讨论】:

标签: java cmd runtime.exec


【解决方案1】:

默认情况下,PrintStreams 不会自动刷新写入它们的内容。这意味着您写入它的数据不会立即发送到它所环绕的流中。但是,如果您使用new PrintStream(p.getOutputStream(), true) 构造PrintStream,它将在调用任何 println 方法、写入字节数组或写入换行符时自动刷新。这样,您写入的任何内容都将立即可供进程访问。

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html

【讨论】:

  • 这不是这里的问题。
  • 好的,我已经完成了new PrintStream(p.getOutputStream(), true),但仍然没有在 cmd 上收到消息,为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-01
  • 2015-09-07
  • 2014-08-21
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多