File System Change Monitoring through Java

最后都变了- 提交于 2019-12-10 15:17:46

问题


I want to monitor changes with selected directories on my system through java. I have bit idea about watchservice in java 7. But watchservice only returns directory name to which change event (MODIFY, DELETE) occurs. On the other side, I want all information about change like user who made changes, time at which change takes place etc.

It is something like a want to read change journals on NTFS file system. Is there any other way available in Java to record such changes asynchronously ? ...

Thanks in advance.


回答1:


Not sure if you have the capability to add import libraries to your project but commons-io has a FileAlterationObserver that may suit your needs:

http://commons.apache.org/io/api-release/org/apache/commons/io/monitor/FileAlterationObserver.html




回答2:


if you are using java7 have a look at java.nio.file.FileSystem and WatchService

for more info refer Oracle tutorial




回答3:


If you just want to get notified when something happens you can use one of the libraries posted in the answers.

If you want to do it by yourself using a small example (and you're limited to Windows) take a look at this article about Java Native Interface. Using the ReadDirectoryChanges function you can decide to receive asynchronous notifications.

If you really need to know WHO made that changes then...good luck! For what I know (and for Windows only) this isn't a simple topic because Windows doesn't save that information. You can get it via JNI with Object Auditing but you have to enable it and it'll slow down a little bit your system. Take a look at this post for some details.




回答4:


You might find Event Programming Example: Google Guava EventBus and Java 7 WatchService useful. I'm trying to do asynchronous file watching as well and am likely going to use some derivative of this.

Or, if you are willing to skip direct management of the WatchService, give the Camel file endopoint a try.



来源:https://stackoverflow.com/questions/10008351/file-system-change-monitoring-through-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!