如何在Java中监视外部文件

如何在Java中监视外部文件,java,jakarta-ee,Java,Jakarta Ee,我有一个.exe文件,它将.txt文件作为输入,并将.txt文件作为输出返回 我有两个文件夹名称是InputFiles和ExeFolder InputFiles文件夹有太多的输入文件,这些文件作为参数传递给.Exe文件 exe文件夹有.exe文件,输出文件,只有一个输入文件(我们将从输入文件文件夹获取此文件) 我想构建一个web应用程序,它将按以下方式工作 public static void main(String[] args) throws IOException { //created

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

我有两个文件夹名称是
InputFiles
ExeFolder

InputFiles
文件夹有太多的输入文件,这些文件作为参数传递给
.Exe
文件

exe文件夹
.exe
文件,
输出
文件,只有一个
输入
文件(我们将从
输入文件
文件夹获取此文件)

我想构建一个web应用程序,它将按以下方式工作

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"
                                        );
 }
步骤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;
}
  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);
    }
 }
如果你观察,我的代码,除了
.exe
监控之外,所有的事情都完成了,不管它是否完成。一旦第一次迭代过程完成,它就允许下一次迭代。在第二次迭代中,
.exe
将是过程。它就像
队列
一样

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

我什么也没找到

有人能帮我吗

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


谢谢

在从java运行.exe的过程中学习本快速教程。足够(4页)

范例

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

要运行外部进程(本例中为
.exe
程序),请忘记
Runtime.exec()
。取而代之的是使用;声明这是目前启动子流程的首选方式。

很抱歉,我们不会编写您的代码。尝试一些东西,如果你有问题,问us@PhilippSander我写了两个函数。但我面临一些问题,所以我会在1小时内告诉你。@PhilippSander我发布了以下问题。你能检查一下吗<代码>http://stackoverflow.com/questions/17808813/how-to-return-array-values-as-a-output-to-a-method-in-java@PhilippSander抱歉,太晚了,我用一些函数修改了我的问题。你能检查一下吗。@PhilippSander我又用一个函数修改了我的问题。你能检查一下吗。
Runtime.exec()
出于实用目的,不推荐使用。现在运行外部进程的正确方法是使用
ProcessBuilder
,请参阅我的答案。很抱歉,我现在没有
.exe
文件。我会告诉你们,在这段时间里,我会阅读你们建议的教程。告诉我一件事,我需要在代码中的什么地方提到我的
.exe
文件路径。请使用程序的完整路径或使用带有不同参数的.exec()。或者像Oscar Lopez建议的那样使用ProcessBuilder。它看起来更易于使用和管理;)@user1394628我的
ThMapInfratab1-2.exe
位于
C:/Users/Infratab Bangalore/Desktop/Rod的
目录下。知道我想运行这个
.exe
文件。我尝试了以下方法<代码>运行时rt=Runtime.getRuntime();String[]filePath={“C:/Users/Infratab Bangalore/Desktop/Rod's”};Process proc=rt.exec(“ThMapInfratab1-2.exe”,文件路径)。但它不起作用。我如何修复这个问题。我想对我来说是这样的:File dir=new File(“C:/Users/Infratab Bangalore/Desktop/Rod”)rt.exec(“ThMapInfratab1-2.exe”,null,dir);洛佩兹:我修改了我的问题。你能检查一下吗?洛佩兹:我修改了我的问题。你能检查一下吗。