如何在Java中监视外部文件

时间:2013-07-23 08:12:38

标签: java java-ee

我有一个.exe文件,它将.txt文件作为输入,它将.txt个文件作为输出返回。

我有2个文件夹名称InputFilesExeFolder

InputFiles文件夹包含大量输入文件,这些文件作为参数传递给.Exe文件。

ExeFolder包含.exe个文件,output个文件和只有一个Input文件(我们将从InputFiles文件夹中获取此文件)。

我想构建1个Web应用程序,它将按以下方式工作。

第1步:

它检查,在我的要求中,sourceDirectory中有多少文件可用。通常每次我想查找.txt文件时,但为了我的代码维护,我将filetype作为参数传递给函数

为此,我编写了以下代码,它工作正常

 public List<File> ListOfFileNames(String directoryPath,String fileType)
{
    //Creating Object for File class
    File fileObject=new File(directoryPath);
    //Fetching all the FileNames under given Path
    File[] listOfFiles=fileObject.listFiles();
    //Creating another Array for saving fileNames, which are satisfying as far our requirements
    List<File> fileNames = new ArrayList<File>();
    for (int fileIndex = 0; fileIndex < listOfFiles.length; fileIndex++) 
    {
        if (listOfFiles[fileIndex].isFile())
        {
          //True condition,Array Index value is File
          if (listOfFiles[fileIndex].getName().endsWith(fileType)) 
          {
              //System.out.println(listOfFiles[fileIndex].getName());
              fileNames .add(listOfFiles[fileIndex]);
          }
        }  
    }
    return fileNames;
}

第2步:

我使用for循环基于ListOfFileNames[dir,filetype]的长度,每个迭代file都会在ExeFolder文件夹中覆盖。为此,我写了以下函数。它工作正常

  public void FileMoving(File sourceFilePath,String destinationPath,String fileName)throws IOException 
 {
File destinationPathObject=new File(destinationPath);
if (
        (destinationPathObject.isDirectory())&&
        (sourceFilePath.isFile())
    )
    //both source and destination paths are available 
    {
        //creating object for File class
        File statusFileNameObject=new File(destinationPath+"/"+fileName);
        if (statusFileNameObject.isFile())
            //Already file is exists in Destination path
            {
                //deleted File
                statusFileNameObject.delete();
                //paste file from source to Destination path with fileName as value of fileName argument
                FileUtils.copyFile(sourceFilePath, statusFileNameObject);
            }
            //File is not exists in Destination path.
            {
                //paste file from source to Destination path with fileName as value of fileName argument
                FileUtils.copyFile(sourceFilePath, statusFileNameObject);
            }
    }
}

第3步:

.exe文件将被运行。为此,我编写了以下函数。工作正常但在此函数中我需要添加一些代码进行等待。

   public void ExeternalFileProcessing(String DirectoryPath,String exeFilePath,String inputFileName) throws IOException
  {
//Creating Absolute file path of the executableFile
String executableFileName = DirectoryPath+"/"+exeFilePath;
//Assinging the InputFileName argument value to inputFile Variable
String inputFile=inputFileName;
//creating ProcessBuilderObject with 2 arguments
ProcessBuilder processBuilderObject=new ProcessBuilder(executableFileName,inputFile);
//creating object
File absoluteDirectory = new File(DirectoryPath);
//Assinging 
processBuilderObject.directory(absoluteDirectory);
//starting process
processBuilderObject.start();
//
//processBuilderObject.wait();
 }

第4步:

完成.exe进程后,只有下一次迭代才会开始。这意味着我们需要监控.exe进程,无论是否完成。

对于集成,我将以下函数名称写为Integration

  public void Integration(String fileType,String sourcePath,String directoryPath,String executableName,String inputFileName)throws IOException
  {
    //created object for Class
    ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
    //calling Method from class object
    List<File> finalListNames=ExternalFileExecutionsObject.ListOfFileNames(sourcePath,fileType);
    for (int fileIndex = 0; fileIndex < finalListNames.size(); fileIndex++) 
    {
        //Copy and pasting file from SourcePath to destination Path
        ExternalFileExecutionsObject.FileMoving(
                                                    finalListNames.get(fileIndex),
                                                    directoryPath,
                                                    inputFileName
                                                );
        //Form here,.exe process will be start
        ExternalFileExecutionsObject.ExeternalFileProcessing(directoryPath,executableName,inputFileName);
    }
 }

我通过以下方式在Integration中调用了这些main()函数。

public static void main(String[] args) throws IOException
{
//created object for Class
ExternalFileExecutions ExternalFileExecutionsObject=new ExternalFileExecutions();
ExternalFileExecutionsObject.Integration(
                                            ".txt",
                                            "C:/Users/Infratab Bangalore/Desktop/copy",
                                            "C:/Users/Infratab Bangalore/Desktop/Rods",
                                            "ThMapInfratab1-2.exe",
                                            "TMapInput.txt"
                                        );
 }

如果你是观察者,我的代码,除了.exe监视之外,每件事都已完成,是否完成。一旦完成第一次迭代过程,它就允许下一次迭代。再次进行第二次迭代.exe将是process.it就像queue

实际上我从不在Java工作,但是使用stackoverflow我写了上面的函数。现在我想修复.exe监控。

我没找到任何东西。

任何人都可以帮助我。

我希望,你们明白我所面对的是什么。

由于

2 个答案:

答案 0 :(得分:2)

要运行外部流程(在这种情况下为.exe程序),请忘记Runtime.exec()。而是使用ProcessBuilder; documentation表示,这是目前启动子流程的首选方式。

答案 1 :(得分:0)

关于从java运行.exe的快速教程。它应该足够(4页)

http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1

示例

import java.util.*;
import java.io.*;

public class GoodWindowsExec
{
    public static void main(String args[])
    {


        try
        {            


            Runtime rt = Runtime.getRuntime();

            Process proc = rt.exec("cmd.exe /C ping"); // executing ping through commandshell of windows

            proc.getErrorStream() // errorstream

            proc.getInputStream()  // outputstream


            int exitVal = proc.waitFor(); // wait till process ends    
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}