2012-07-19 177 views
1

我用下面的代码从Java(从here通过代码)如何从java执行cmd命令后定期检查文件?

private static void execute(File file){ 
    try { 
     String[] command = 
     { 
      "cmd", 
     }; 
     Process p = Runtime.getRuntime().exec(command); 
     new Thread(new SyncPipe(p.getErrorStream(), System.err)).start(); 
     new Thread(new SyncPipe(p.getInputStream(), System.out)).start(); 
     PrintWriter stdin = new PrintWriter(p.getOutputStream()); 
     stdin.println("cd " + file.getParent()); 
     stdin.println("gxm -execute " + file.getPath()); 
     // write any other commands you want here 
     stdin.close(); 
     int returnCode = p.waitFor(); 
     System.out.println("Return code = " + returnCode); 
    }catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

SyncPipe类运行CMD命令:

class SyncPipe implements Runnable 
{ 
public SyncPipe(InputStream istrm, OutputStream ostrm) { 
     istrm_ = istrm; 
     ostrm_ = ostrm; 
    } 
    public void run() { 
     try 
     { 
      final byte[] buffer = new byte[1024]; 
      for (int length = 0; (length = istrm_.read(buffer)) != -1;) 
      { 
       ostrm_.write(buffer, 0, length); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    private final OutputStream ostrm_; 
    private final InputStream istrm_; 
} 

此代码工作正常,并执行结果被一个文件称为COMPLETED ,但我已经实现了以下方法来检查该文件以指示执行完成。

private static boolean checkFinished(String path) 
{ 
    boolean result = false; 
    String directory = path + "\\expts"; 
    File dir = new File(directory); 
    if(dir.isDirectory()) 
     while(!result) 
     { 
      for(File f: dir.listFiles()) 
       if(!f.isDirectory() && "__COMPLETED__".equals(f.getName())) 
       { 
        result = true; 
        break; 
       } 
      if(!result) 
      try { 
       Thread.sleep(3000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    System.out.println(result); 
    return result; 

} 

但是当我打电话的主要方法,这些方法是这样的:

  File f = new File("C:\\inputFile.txt"); 
    execute(f); 

    System.out.println(checkFinished(f.getParent())); 

我得到以下输出:

false // printed from the checking 
Microsoft Windows [Version 6.1.7601] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 
.... // rest of cmd output 

这里的问题是,checkFinshed方法是印刷一次,并在文件已经存在的时候打印。代码有什么问题?

回答

0

37个意见,甚至没有评论!无论如何,我改变了我的checkFinished方法以下,它的工作。希望它会让别人受益。

private static boolean checkFinished(String path) 
{ 
    boolean result = false; 
    String directory = path + "\\expts"; 
    File dir = new File(directory); 
    while(true) 
    { 
     try 
     { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     if(dir.isDirectory()) 
     { 
      for(File f: dir.listFiles()) 
       if(!f.isDirectory() && "__COMPLETED__".equals(f.getName())) 
       { 
        result = true; 
        logger.info(" SIMULATOR: simulation finished "); 
        break; 
       } 
      if(result) 
       break; 
     } 
    } 
    return result; 
}