How can I check if a file is already open before trying to delete it programmatically?
something like this
if (file is open){
// close it first
}
// delete file
How can I check if a file is already open before trying to delete it programmatically?
something like this
if (file is open){
// close it first
}
// delete file
I don't think this is going to work for a few reasons.
There are no standard Java mechanisms for testing if you already have a file open.
Even if there were such a mechanism, it would be difficult to find the file's handle so that you could close it.
Even if you could find the file handle, there is a potential race condition where one thread tests a file and attempts to delete it, and a second thread opens the file handle.
None of this addresses the case where some other process has the file open.
If you've got a problem deleting files that your application has opened, then it is most likely that the real problem is that your application is leaking file descriptors. And the best solution to that is to find and fix the leak ... or to make sure that all of your file streams etc are closed using "try / finally" or the new Java 7 "try with resource" construct.
On the other hand, if the file is opened by some other process, then you may as well just try to delete the file without testing to see if it is open. If the delete succeeds, it succeeds. Otherwise detect the failure and do whatever you would have done if you detected that the file was open.
You can find you answer there check if a file is already open as your question is a replicate of that question
Hi @chinchu and @Subash,
You cannot check directly, there isn't any API to do that, you have to write code for it
Below is a code snippet that you can modify as per your requirement:
File file1 = new File("<file path>");
boolean isDeleteSuccess;
do
{
//delete the file
isDeleteSuccess = file1.delete();
if (isDeleteSuccess)
System.out.println(file1.getName() + " is deleted!");
else
{
//if not deleted then wait for 10 seconds for file to be closed
System.out.println("Delete operation is failed.");
Thread.sleep(10000);
}
} while (!isDeleteSuccess);//if not deleted then try again
Basically, you have to keep track of whether you have opened the file if you need to make sure it gets closed.
Note that on Unix systems, deleting an open file is not a problem and will work correctly.
You can try to lock the file and if the file is not opened by someone else you won't get an exception, otherwise you will get an exception, so writing a function like this one you can check if a file is already opened by someone else :
public boolean isFileOpened(File file)
boolean res = false;
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Get an exclusive lock on the whole file
FileLock lock = channel.lock();
try {
//The file is not already opened
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
// File is open by someone else
res = true;
} finally {
lock.release();
}
return res;
}
Then you can use the method to check if the file is already opened in this way :
if(isFileOpened(someFile)) {
//Do Something
}