ITeye博客

StreamGobbler方法

hellsing42 · 2007-08-30 10:24 · j2se基础 · Markdown

class StreamGobbler extends Thread {
  InputStream is;
  String type;
  
  StreamGobbler(InputStream is, String type) {
      this.is = is;
      this.type = type;
  }
  
  public void run() {
      try {
          InputStreamReader isr = new InputStreamReader(is);
          BufferedReader br = new BufferedReader(isr);
          String line=null;
          while ( (line = br.readLine()) != null)
              System.out.println(type + ">" + line);
      } catch (IOException ioe) {
          ioe.printStackTrace();
      }
  }
  // end StreamGobbler

调用:


			
			Process proc = Runtime.getRuntime().exec(moveCommand);
            //any error message?
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
            // any output?
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
            // any error???
            int exitVal = proc.waitFor();
            String executeStatus = "";
            if(exitVal==0) executeStatus = "success..";
            if(exitVal==1) executeStatus = "failure....";
            
            System.out.print(executeStatus);
            

具体原来不知道是如何,但是效果是可以有效的监控Runtime.getRuntime.exec("")是否有效的执行完成,当执行完成后才继续执行下面的程序.希望有其中的原理

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。