I need to write some data in a temporary file and to store this file in a directory A. I use the File.createTempFile method to do this. But, there is a thread that periodically polls the directory A to check if there are temporary files to process.
// create a temporary file that will contain the data
newTmpFile = File.createTempFile("prefix", recoverFileExt, new File(
recoverDirectory));
// the file is set to non readable, so the recovery thread cannot
// access it
newTmpFile.setReadable(false);
//write data into the file
// the file is written, it is set to readable so the recovery thread
// can now access it
newTmpFile.setReadable(true);
The problem is that I don’t want the recovery thread to access the file before the write operation is done. So, I use this mechanism : I create the file, set it as non readable, write to it then set it readable and close it. The problem is that just after the file creation, the file is still readable and the thread can access it.
So, I wanted to know if there is a possibility to set the file as non readable at its creation or if you have other solutions.
Thanks
My suggestion would be to first give the file a different name (e.g. using a different prefix) and rename it after it’s been written.
This way the recovery thread can differentiate between partially- and fully-written files and only process the latter.