如何在Java中监视外部文件 [英] How can I Monitor External files in Java

查看:72
本文介绍了如何在Java中监视外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a .exe file, It takes .txt file as a Input and it returns .txt files as outputs.

我有2个文件夹名称 InputFiles ExeFolder

I have 2 folders names are InputFiles and ExeFolder.

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

InputFiles folder have so many number of input files, which files are passing as a argument to .Exe file.

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

ExeFolder have .exe file,output files and only one Input file(we will get this file from InputFiles folder).

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

I want to build 1 web Application, It will work in the following way.

第1步:

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

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.

为此,我编写了以下代码,它是我的rking fine

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

第2步:

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

I used for loop Based on length of ListOfFileNames[dir,filetype],For every Iteration file will be overwrite in ExeFolder folder. 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);
            }
    }
}

第3步:

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

.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.

   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 进程,无论是否完成。

once .exe process was done, then only next Iteration will be start. That means we need to monitor .exe process,whether is it done or not.

进行集成,我写道以下函数名称为 Integration

for integration, I wrote the following function name as 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()中运行。

I called these, Integration function in my main() in the following way.

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

如果您观察我的代码,除了<$ c $之外,所有事情都已完成c> .exe 监控,无论是否完成。一旦完成第一次迭代过程,它就允许下一次迭代。再次进行第二次迭代 .exe 将是process.it就像 queue

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.

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

Actually I never work on Java,but using stackoverflow I wrote the above functions. Now I want to fix .exe monitoring.

我没找到任何东西。

任何人都可以帮助我。

我希望你们明白我所面对的。

I hope, you guys understand what I am facing.

谢谢

推荐答案

用于运行外部进程( .exe 程序),忘记 Runtime.exec()。而是使用 ProcessBuilder ; 文档说明它是首选的方式这几天开始一个子流程。

For running an external process (a .exe program in this case), forget about Runtime.exec(). Instead use ProcessBuilder; the documentation states that it's the preferred way to start up a sub-process these days.

这篇关于如何在Java中监视外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆