I am calling a C++ Method via JNI which creates two files. A text log file and a pdf file in a given directory. I want to delete these files (if they exist) before executing the JNI method.
I am using Apache commons.io (FileUtils.forceDelete(File file)) for that. When I execute I get an IOException:
java.io.IOException: Unable to delete file: D:\Folder\file.log
It is almost certainly locked by another process. If it is another process locking at the OS level (say you had the file open it a text editor) then you won't have much luck. Even windows explorer can fail to delete a file if something else is locking it. However have a look at
java.nio.channels.FileLock for the relevant API calls.