java - 如何在 Java 中监控外部文件

标签 java jakarta-ee

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

我有 2 个文件夹,名称分别是 InputFilesExeFolder

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

ExeFolder.exe文件,输出文件和只有一个输入文件(我们将得到这个文件来自 InputFiles 文件夹)。

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

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

我通过以下方式在我的 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"
                                        );
 }

如果你观察一下,我的代码,除了.exe监控之外,所有事情都完成了,是否完成。一旦第一次迭代过程完成,就允许下一次迭代。在第二次迭代中再次.exe 将是 process.it,就像 queue 一样。

实际上我从来没有使用过Java,但是使用stackoverflow我编写了上述函数。现在我想修复 .exe 监控。

我没有发现任何东西。

谁能帮帮我。

我希望你们能理解我所面临的问题。

谢谢

最佳答案

对于运行外部进程(在本例中为 .exe 程序),请忘记 Runtime.exec()。而是使用 ProcessBuilder ; documentation指出这是目前启动子流程的首选方式。

关于java - 如何在 Java 中监控外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17804790/

相关文章:

java - 生成随机二进制数据,例如 23ABFF

java - 同一 Java EE session Bean 中的数据源和 EntityManager

eclipse - 如何在 Eclipse web 项目中安装 JDBC 驱动程序而不会遇到 java.lang.ClassNotFoundexception

eclipse - 如何将 JAX-RS 添加到现有项目

java - 为什么将方法参数重新分配给局部变量?

java - 哪个 Java EE 服务器?

java - 未找到 key 保护算法 : java. security.KeyStoreException:证书链未验证

java - 用于 Java/Spring 配置的 ZooKeeper?

jsf - 是否有任何理由在 CDI session 范围 bean 中使用有状态 EJB

java - 如何在 Metro/JavaEE 自动生成的 WSDL 中自定义命名空间前缀?