Here's the part of the code that is problematic:
FileChannel fileChannel = FileChannel.open(filePath, StandardOpenOption.WRITE);
fileChannel.force(true);
FileLock lock = fileChannel.lock();
fileChannel.truncate(0);
fileChannel.write(buffer);
lock.release();
fileChannel.close();
buffer
is a ByteBuffer
that was filled with some data prior to this code.
So, this code is done periodically in one thread and no other threads are using this lock or accesing the same file. What happens is when I access the file with notepad while the program is running, I sometimes get the OverlappingFileLockException
. If I catch that exception, the thread will loop and generate the same exception over and over again, even if I close the notepad. I also sometimes get the error: The requested operation cannot be performed on a file with a user-mapped section open
, which might or might not be related to OverlappingFileLockException
, but it sometimes happens for the same reason, when I open the file with notepad or open file properties while the program is running.