黑马程序员技术交流社区

标题: 往另一个进程的标准输出流输出数据,另一进程没有运行? [打印本页]

作者: 真实姓名被注册    时间: 2015-7-20 14:34
标题: 往另一个进程的标准输出流输出数据,另一进程没有运行?
本帖最后由 真实姓名被注册 于 2015-7-20 14:36 编辑

  1. <P>import java.util.*;
  2. import java.io.*;
  3. public class Temp{
  4. public static void main(String[] args){
  5. System.out.println("执行Temp");
  6. File file=new File("hehe.txt");
  7. try {
  8. System.setOut(new PrintStream(
  9. new BufferedOutputStream(
  10. new FileOutputStream("hehe.txt"))));
  11. } catch (FileNotFoundException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. }
  15. System.out.println("abc");
  16. System.out.flush();
  17. }
  18. }
  19. import java.io.*;
  20. public class Test{
  21. public static void command(String command){
  22. try{
  23. Process process=new ProcessBuilder(command.split(" ")).
  24. start();
  25. System.out.println(process);
  26. PrintWriter out=new PrintWriter(
  27. new OutputStreamWriter(
  28. process.getOutputStream()));
  29. out.println("def");

  30. out.flush();
  31. }catch(IOException e){e.printStackTrace();}
  32. }
  33. public static void main(String[] args){
  34. Test.command("java Temp");
  35. }

  36. }</P>
复制代码

我想在Test中启动Temp,获得Temp重定向过的out,就是文件“hehe.txt”,然后在Test中继续往文件写入,为什么Temp没有执行?






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2