日期:2014-05-20  浏览次数:20644 次

父进程捕获子进程的输出问题
test.java  
class   test
{  
    public   static   void   main(String   args[])  
    {  
          System.out.print( "aaaaa ");
          System.out.print( "bbbbb ");
          System.out.print( "ccccc ");
          System.out.print( "\n ");
          ..........       //一系列的print与println的交替无序使用
          System.out.println( "aaaaa ");
          System.out.println( "bbbbb ");
          System.out.println( "ccccc ");          
    }  
}  


test1.java
import   java.io.*;
public   class   test1
{
    public   static   void   main(String[]   args)
    {
    Process   p=null;
        Runtime   run   =   Runtime.getRuntime();
        String   cmd;
        try
        {
          cmd= "cd   F:\\java ";
              run.exec( "cmd   /c   "+cmd);
              cmd= "javac   test.java ";
              p   =   run.exec( "cmd   /c   "+cmd);
              try
              {
                    p.waitFor();
              }
              catch(InterruptedException   e1){}
              p   =   run.exec( "cmd   /c   "+ "java   test ");  
              InputStreamReader   ir=new   InputStreamReader(p.getInputStream());  
              LineNumberReader   input   =   new   LineNumberReader   (ir);  
              String   all= " ";
              String   line;  
              while((line   =   input.readLine   ())   !=   null)  
              {
                      all=all+line;     //这里有问题,无法实现与子进程一样的格式
              }
              System.out.println(all);  
        }
        catch(IOException   e)
        {}
    }
}

怎样才能让父进程的输出跟子进程的输出一样

------解决方案--------------------
public String readLine() throws IOException
读取文本行。可认为行是由换行符( '\n ')、回车符( '\r ')或回车后面紧跟换行符中的任何一个终止的。

覆盖:
类 BufferedReader 中的 readLine
返回:
包含行的内容且不包括任何行结束符的字符串,如果已到达流的末尾,则返回 null