[英]How can I Monitor External files in Java
我有一個.exe
文件,它將.txt
文件作為輸入,它返回.txt
文件作為輸出。
我有2個文件夾名稱是InputFiles
和ExeFolder
。
InputFiles
文件夾有很多輸入文件,這些文件作為參數傳遞給.Exe
文件。
ExeFolder
有.exe
文件, output
文件和只有一個Input
文件(我們將從InputFiles
文件夾中獲取此文件)。
我想構建1個Web應用程序,它將以下列方式工作。
第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();
}
第四步:
一旦完成.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 ; 文檔說明這是現在啟動子流程的首選方式。
關於從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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.