java - 从java执行cmd命令后如何定期检查文件?

标签 java cmd

我使用下面的代码从java运行cmd命令(代码来自here)

  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 类:

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_;
}

这段代码运行正常,执行结果是一个名为 <strong>COMPLETED</strong> 的文件,但我已经实现了以下方法来检查该文件以指示执行已完成。

  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方法是打印一次并在文件已经存在时打印 true 。代码有什么问题吗?

最佳答案

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;
}

关于java - 从java执行cmd命令后如何定期检查文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561533/

相关文章:

java - 使用 Java GET 从客户端接收链接列表

Java NIO 和 Windows 磁盘访问

java - JLayeredPane 不更新 JLabel 的移动

windows - 获取批处理脚本中特定路径的父目录

windows - 如何使用 VB6 或 BAT/CMD 列出连接到我的 WiFi 网络的设备?

CMD 不支持 UNC 路径作为当前目录

java - 与平面 if 语句相比,嵌套 if 语句是否可以提高性能或具有其他优势?

java - 如何在java中创建半透明窗框

windows - 批处理文件存储行变量并对它们求和

windows - for 循环的批处理脚本不起作用,仅循环一次