0

Why would obtaining a RandomAccessFile(path.toFile(),"rw") produce a FileNotFoundException from a Path that was returned from WatchService and the WatchEvent kind is ENTRY_CREATE?

java.io.FileNotFoundException: 
C:\SharedFolders\FromS01\SSLServer01\SSLInternal02\DOMAIN\EML\E6046292.723
(The process cannot access the file because it is being used by another process)
    at java.io.RandomAccessFile.open(Native Method) ~[na:1.8.0-ea]
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:236) ~[na:1.8.0-ea]
    at cbbb.filejobs.Main.fileCopied(Main.java:446) ~[CbbbFileJobs.jar:na]
    at cbbb.filejobs.Main.processFilesPending(Main.java:409) ~[CbbbFileJobs.jar:na]
    at cbbb.filejobs.Main.handleFileTasks(Main.java:156) ~[CbbbFileJobs.jar:na]
The Coordinator
  • 13,007
  • 11
  • 44
  • 73
  • The file is a valid file. I can open it in notepad. Sometimes it is FileNotFound, other times truncated, other times fine. Posted exception – The Coordinator Dec 05 '13 at 17:50
  • 2
    You noticed this part, right?: `(The process cannot access the file because it is being used by another process)` – keyser Dec 05 '13 at 17:52
  • Yes, but there is no reason for that when opening a RandomAccessFile on Windows because shared locking is only advisory. Even if another file is working, then it should allow the file to be opened. No? – The Coordinator Dec 05 '13 at 17:54
  • Where did you get that info? On Windows file locking is mandatory, on UNIX it is advisory/ – Uku Loskit Dec 05 '13 at 18:04
  • We have verified that Windows NT shared locking is only advisory. – The Coordinator Dec 05 '13 at 18:10
  • You have it backwards I think, UNIX enforces, Windows does not unless it is a system file. – The Coordinator Dec 05 '13 at 18:25
  • You are opening for "rw" = read & write - if the file is already being opened in "rw" mode by another application that is supposed to fail. Do you get a different behaviour with Java 7? – assylias Dec 06 '13 at 08:38
  • It all was working fine (for last month) until last update. It looks like the routine I am using with Lambdas is so fast that I am getting the file handle even though the system is still in an intermediate state (The process cannot access the file because it is being used by another process) ... will have to find a work-around ... – The Coordinator Dec 06 '13 at 09:01
  • @assylias Actually, Opening a `RandomAccessFile` in "rw" mode is required for "rw" functionality. It will not fail if the file is locked by another OS process -- it means that you will use it for those purposes. That has nothing to do with the locking and file sharing mechanism. The lock is tried and obtained from the `FileChannel` provided by the `RandomAccessFile`. – The Coordinator Dec 08 '13 at 08:13

1 Answers1

0

"The process cannot access the file because it is being used by another process"

It turns out that with a WatchService event, the file is not necessarily usable the instant it is created.

Sometimes the operating system has the file status in an intermediate state the moment it has a file create event.

The solution, in my case, is to:

1) Watch for the MODIFIED event. All files with content will have a CREATED followed by a MODIFIED.

2) Check that the file exists again (in case it was DELETED immediately after CREATED). And then retry to get a RandomAccessFile on it. This never happened but I suppose it could -theoretically.

3) If the file was deposited using operating system exclusive/shared locking. Then the lock will not be obtainable if the other process still has a handle on it.

What is funny .... buggy actually .. is that I was previously able to LOCK the file exclusive (and shared worked) when it was just CREATED. But the file content was always null.

So, it seems that LOCKING immediately after CREATE does not always work. Waiting for MODIFIED before trying to lock seems to solve the problem.

Really, the exception should not be FileNotFound, ,but FileInUse or FileNotAccessible ...

The Coordinator
  • 13,007
  • 11
  • 44
  • 73
  • tried it out but still get the same Error when i moved my reading from the created event handling to the modified event handling – tung Jun 03 '15 at 13:05