简体   繁体   English

如何在Java中监视外部文件

[英]How can I Monitor External files in Java

I have a .exe file, It takes .txt file as a Input and it returns .txt files as outputs. 我有一个.exe文件,它将.txt文件作为输入,它返回.txt文件作为输出。

I have 2 folders names are InputFiles and ExeFolder . 我有2个文件夹名称是InputFilesExeFolder

InputFiles folder have so many number of input files, which files are passing as a argument to .Exe file. InputFiles文件夹有很多输入文件,这些文件作为参数传递给.Exe文件。

ExeFolder have .exe file, output files and only one Input file(we will get this file from InputFiles folder). ExeFolder.exe文件, output文件和只有一个Input文件(我们将从InputFiles文件夹中获取此文件)。

I want to build 1 web Application, It will work in the following way. 我想构建1个Web应用程序,它将以下列方式工作。

step 1 : 第1步 :

it checks, How many no of files are available in sourceDirectory as far my requirements.generally every time I want to find .txt files but for my code maintenance I passed filetype as a argument to function. 它检查,sourceDirectory中有多少文件可用于我的要求。通常每次我想找到.txt文件但是为了我的代码维护,我将filetype作为参数传递给函数。

For this,I wrote the following code,it's working fine 为此,我编写了以下代码,它工作正常

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

step 2: 第2步:

I used for loop Based on length of ListOfFileNames[dir,filetype] ,For every Iteration file will be overwrite in ExeFolder folder. 我用for基于的长度循环ListOfFileNames[dir,filetype] ,对于每一个迭代file将在重写ExeFolder文件夹中。 For this I wrote the following function.It's working fine 为此我写了以下函数。它工作正常

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

Step 3 : 第3步:

.exe 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. .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();
 }

Step 4: 第四步:

once .exe process was done, then only next Iteration will be start. 一旦完成.exe进程,那么只有下一次迭代才会开始。 That means we need to monitor .exe process,whether is it done or not. 这意味着我们需要监视.exe进程,无论是否完成。

for integration, I wrote the following function name as Integration . 为了集成,我将以下函数名称写为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);
    }
 }

I called these, Integration function in my main() in the following way. 我通过以下方式在main()中调用了这些Integration函数。

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

If you observer, my code,every thing was done except .exe monitoring,whether is it completed or not.Once first iteration process was done then it allows next Iteration.In second Iteration again .exe will be process.it just like queue . 如果你是观察者,我的代码,除了.exe监视之外,每件事都已完成,无论是否完成。一旦完成第一次迭代过程,它就允许下一次迭代。再次迭代迭代.exe将是process.it就像queue一样。

Actually I never work on Java ,but using stackoverflow I wrote the above functions. 实际上我从不在Java工作,但是使用stackoverflow我写了上面的函数。 Now I want to fix .exe monitoring. 现在我想修复.exe监控。

I didn't found anything. 我没找到任何东西。

can anyone help me. 谁能帮我。

I hope, you guys understand what I am facing. 我希望,你们明白我所面对的是什么。

Thanks 谢谢

For running an external process (a .exe program in this case), forget about Runtime.exec() . 要运行外部进程(在这种情况下是.exe程序),请忘记Runtime.exec() Instead use ProcessBuilder ; 而是使用ProcessBuilder ; the documentation states that it's the preferred way to start up a sub-process these days. 文档说明这是现在启动子流程的首选方式。

Follow this quick tutorial over running .exe from java. 关于从java运行.exe的本快速教程。 It should be sufficient (4pages) 应该足够了(4页)

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

example

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM