开发者

can we determine at runtime whether a file is deleted from hard disk or not using java

I am creating my project in which I am indexing all files of m开发者_如何学Pythony PC in a database. Now I want to know at run-time whether a file is deleted, moved or renamed so that I can update the database without doing indexing again.


If you want to know whether a particular file that you already know existed no longer exists, you can just use File.exists(). There are directory watcher packages that will trigger events when files are added/deleted/moved, but running a directory watcher thread on every directory in your file system sounds very inefficient.


Until JDK 7 comes around, there is no non-native mechanism in Java to detect file related events (including deletions). That said, even if you were able to detect file deletions, you'd still have to have some algorithm for bringing your indexes into sync when the application starts.

When we do this sort of thing, we generally capture the path+modified date+length of a file in an index (we use jdbm btrees). Re-indexing is then a matter of iterating the folder tree and the index at the same time. When you find an entry in the index that isn't in the folder tree, remove it from the index. When you find an entry in the folder tree that isn't in the index, add it to the index. If you find an entry that's in both, check the modified date and size.

So until we have jdk7, you are pretty much stuck with a polling based solution (i.e. update the indexes every XX seconds).

Note that jdk7 (in addition to folder monitoring) will have substantial performance improvements in iterating files (the current File based operations are really slow compared to what you can do natively).


I think java 7 may have what you are looking for once it is releases. Refer to the following link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜