I have a java thread A that continously polls a folder RESULTFOLDER and checks if there are some new files present in it.
Now the files are posted by some other program running on another machine into RESULTFOLDER.Now the files posted are all xml files (only xml).so at any point RESULTFOLDEr can hold only xml files.
Now my thread A continiously polls the RESULTFOLDER and parses the xml files one at a time and then deletes it.
Now sometimes what happenes is that if thread A tries to read and parse the file A at the time the other program is posting the file A .In this case i get exception in parsing file.Saying pre mature end of file.
How can i resolve the problem?
One way i think is to check date time of file creation and ensure that file is presnt at least for 1 minute or so.But i dont think java provides such API.How can i go about solving this problem?
You can write the .xml file to the folder, and then write a separate control file written after that. The control file would have zero bytes, and have a different extension, such as .ctl, but would have the same first part of the name.
When the thread polling the result folder finds the .ctl file, it knows it is safe to open the same-named file with a .xml extension.
This approach has the added benefit that it will work even when the writing task is on another computer.