title: How can I Monitor External files in Java
body: <p>I  have a <code>.exe</code> file, It takes <code>.txt</code> file as a Input and it returns <code>.txt</code> files as outputs.</p>
  
  <p>I have 2 folders names are <code>InputFiles</code> and <code>ExeFolder</code>.</p>
  
  <p><code>InputFiles</code> folder have so many number of input files, which files are passing as a argument to <code>.Exe</code> file.</p>
  
  <p><code>ExeFolder</code> have <code>.exe</code> file,<code>output</code> files and only one <code>Input</code> file(we will get this file from <code>InputFiles</code> folder).</p>
  
  <p>I want to build 1 web Application, It will work in the following way.</p>
  
  <p>step 1 :</p>
  
  <p>it checks, How many no of files are available in sourceDirectory as far my requirements.generally every time I want to find <code>.txt</code> files but for my code maintenance I passed <code>filetype</code> as a argument to function.</p>
  
  <p>For this,I wrote the following code,it's working fine</p>
  
  <pre><code> public List&lt;File&gt; 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&lt;File&gt; fileNames = new ArrayList&lt;File&gt;();
      for (int fileIndex = 0; fileIndex &lt; 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;
  }
  </code></pre>
  
  <p>step 2:</p>
  
  <p>I used <code>for</code> loop Based on length of <code>ListOfFileNames[dir,filetype]</code>,For every Iteration <code>file</code> will be overwrite in <code>ExeFolder</code> folder. For this I wrote the following function.It's working fine</p>
  
  <pre><code>  public void FileMoving(File sourceFilePath,String destinationPath,String fileName)throws IOException 
   {
  File destinationPathObject=new File(destinationPath);
  if (
          (destinationPathObject.isDirectory())&amp;&amp;
          (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);
              }
      }
  }
  </code></pre>
  
  <p>Step 3 :</p>
  
  <p><code>.exe</code> file will be run.For this I wrote the following function.Working fine but in this function I need to add some code for waiting.</p>
  
  <pre><code>   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();
   }
  </code></pre>
  
  <p>Step 4: </p>
  
  <p>once <code>.exe</code> process was done, then only next Iteration will be start. That means we need to monitor <code>.exe</code> process,whether is it done or not.</p>
  
  <p>for integration, I wrote the following function name as <code>Integration</code>.</p>
  
  <pre><code>  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&lt;File&gt; finalListNames=ExternalFileExecutionsObject.ListOfFileNames(sourcePath,fileType);
      for (int fileIndex = 0; fileIndex &lt; 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);
      }
   }
  </code></pre>
  
  <p>I called these, <code>Integration</code> function in my <code>main()</code> in the following way.</p>
  
  <pre><code>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"
                                          );
   }
  </code></pre>
  
  <p>If you observer, my code,every thing was done except <code>.exe</code> monitoring,whether is it completed or not.Once first iteration process was done then it allows next Iteration.In second Iteration again <code>.exe</code> will be process.it just like <code>queue</code>.</p>
  
  <p>Actually I never work on <code>Java</code>,but using <code>stackoverflow</code> I wrote the above functions. Now I want to fix <code>.exe</code> monitoring.</p>
  
  <p>I didn't found anything.</p>
  
  <p>can anyone help me.</p>
  
  <p>I hope, you guys understand what I am facing.</p>
  
  <p>Thanks</p>