我有一个 .exe
文件,它以 .txt
文件作为输入,并返回 .txt
文件作为输出。
我有 2 个文件夹,名称分别是 InputFiles
和 ExeFolder
。
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/