如何在Java中监视外部文件

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

我有2个文件夹名称是InputFilesExeFolder

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

ExeFolder.exe文件, output文件和只有一个Input文件(我们将从InputFiles文件夹中获取此文件)。

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

步骤1 :

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

为此,我编写了以下代码,它运行正常

  public List 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 fileNames = new ArrayList(); 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]长度,因为每个Iteration 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 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 ; 文档说明这是现在启动子流程的首选方式。

关于从java运行.exe的本快速教程。 应该足够了(4页)

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

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