FileObserver for transfer events

427 Views Asked by At

I have a directory to which user may transfer files from another device. I have a FileObserver watching that directory. But I don’t know what to watch for. I need to know when the file has finished transferring so I can move it to another location. So what event code do I watch for? FileObserver.CREATE | FileObserver.MODIFY don’t seem to do it.

UPDATE

Based on some logging I make the following observations: for each new file that is transferred to the directory, I get the following events:

FileObserver.MOVED_TO : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG
FileObserver.OPEN : DSC_0058.JPG
FileObserver.CLOSE_NOWRITE : DSC_0058.JPG

So after each move I see a series of OPEN then CLOSE_NOWRITE. I am not sure of the importance of the OPEN then CLOSE_NOWRITE events.

1

There are 1 best solutions below

5
On

I would try CLOSE_WRITE, because the thing doing the downloading will have to be writing and eventually close the file. That said, there is no guarantee that the writer will not close the file before it's complete (it could reopen and append more). So be sure to test it to your satisfaction.