How can I Monitor External files in Java -
i have .exe
file, takes .txt
file input , returns .txt
files outputs.
i have 2 folders names inputfiles
, exefolder
.
inputfiles
folder have many number of input files, files passing argument .exe
file.
exefolder
have .exe
file,output
files , 1 input
file(we file inputfiles
folder).
i want build 1 web application, work in following way.
step 1 :
it checks, how many no of files available in sourcedirectory far requirements.generally every time want find .txt
files code maintenance passed filetype
argument function.
for this,i wrote following code,it's working fine
public list<file> listoffilenames(string directorypath,string filetype) { //creating object file class file fileobject=new file(directorypath); //fetching filenames under given path file[] listoffiles=fileobject.listfiles(); //creating array saving filenames, satisfying far our requirements list<file> filenames = new arraylist<file>(); (int fileindex = 0; fileindex < listoffiles.length; fileindex++) { if (listoffiles[fileindex].isfile()) { //true condition,array index value file if (listoffiles[fileindex].getname().endswith(filetype)) { //system.out.println(listoffiles[fileindex].getname()); filenames .add(listoffiles[fileindex]); } } } return filenames; }
step 2:
i used for
loop based on length of listoffilenames[dir,filetype]
,for every iteration file
overwrite in exefolder
folder. wrote 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 , destination paths available { //creating object file class file statusfilenameobject=new file(destinationpath+"/"+filename); if (statusfilenameobject.isfile()) //already file exists in destination path { //deleted file statusfilenameobject.delete(); //paste file source destination path filename value of filename argument fileutils.copyfile(sourcefilepath, statusfilenameobject); } //file not exists in destination path. { //paste file source destination path filename value of filename argument fileutils.copyfile(sourcefilepath, statusfilenameobject); } } }
step 3 :
.exe
file run.for wrote following function.working fine in function need add code waiting.
public void exeternalfileprocessing(string directorypath,string exefilepath,string inputfilename) throws ioexception { //creating absolute file path of executablefile string executablefilename = directorypath+"/"+exefilepath; //assinging inputfilename argument value inputfile variable string inputfile=inputfilename; //creating processbuilderobject 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(); }
step 4:
once .exe
process done, next iteration start. means need monitor .exe
process,whether done or not.
for integration, wrote following function name integration
.
public void integration(string filetype,string sourcepath,string directorypath,string executablename,string inputfilename)throws ioexception { //created object class externalfileexecutions externalfileexecutionsobject=new externalfileexecutions(); //calling method class object list<file> finallistnames=externalfileexecutionsobject.listoffilenames(sourcepath,filetype); (int fileindex = 0; fileindex < finallistnames.size(); fileindex++) { //copy , pasting file sourcepath destination path externalfileexecutionsobject.filemoving( finallistnames.get(fileindex), directorypath, inputfilename ); //form here,.exe process start externalfileexecutionsobject.exeternalfileprocessing(directorypath,executablename,inputfilename); } }
i called these, integration
function in main()
in following way.
public static void main(string[] args) throws ioexception { //created object 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" ); }
if observer, code,every thing done except .exe
monitoring,whether completed or not.once first iteration process done allows next iteration.in second iteration again .exe
process.it queue
.
actually never work on java
,but using stackoverflow
wrote above functions. want fix .exe
monitoring.
i didn't found anything.
can me.
i hope, guys understand facing.
thanks
for running external process (a .exe
program in case), forget runtime.exec()
. instead use processbuilder; documentation states it's preferred way start sub-process these days.
Comments
Post a Comment