4

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.

Shailesh_B
  • 41
  • 1
  • 3

4 Answers4

3

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

alexwen
  • 1,128
  • 7
  • 16
  • +1 It's a very nice library, he can't get when something changed but he can't know who made what. – Adriano Repetti Apr 04 '12 at 09:30
  • 1
    This one is good library. But it sounds that it uses polling technique while I want asynchronous events for filesystem change. Anyhow, I will explore this lib more. Yes, I have capability to import add libraries. – Shailesh_B Apr 04 '12 at 09:48
3

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

for more info refer Oracle tutorial

Anish Dasappan
  • 415
  • 2
  • 9
0

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.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Thanks. Information provided is very helpful. Actually i am looking for JNI call to do same but NativeCall you mentioned very much matched with my requirement. – Shailesh_B Apr 04 '12 at 10:22
  • @Shailesh_B There isn't a JNI call to do that, otherwise it would be listed the JNI specification, and if there isn't a native Windows API that does it either, it can't be done. – user207421 Apr 05 '12 at 00:06
  • @EJP yes, the "risk" is to have to write a small native DLL to do the dirty job with an exposed "simple" C API – Adriano Repetti Apr 05 '12 at 07:20
  • @Adriano I don't understand any of that. The original post is tagged with JNI so the OP must already be contemplating that. I don't see why you need an 'exposed "simple" C API', you just need the native Java method that is implemented by your JNI, which calls whatever OS APIs exist. If they don't exist, you can't do it at all. – user207421 Apr 06 '12 at 09:54
  • @EJP I guess I've not been so clear! What I mean is: it can be done with native code (even if with some effort) then he'll just have to write few C functions for JNI interface. – Adriano Repetti Apr 06 '12 at 10:23
  • @Adriano Of course but I don't see where the '"simple" C API' comes into it. Whatever API it exposes will be completely defined by the signatures of the corresponing native methods. – user207421 Apr 07 '12 at 06:51
  • @EJP yes, "simple" if compared with the "true" native API. I guess he won't need all parameters and features from them. – Adriano Repetti Apr 10 '12 at 07:22
  • I still don't know what you mean. There will be a Java/JNI API defined by Java native methods, which aren't exactly 'simple' at the JNI C level, and an operating system API defined by the operating system, which is whatever it is. What your third 'simple' API is remains a mystery. – user207421 Apr 10 '12 at 09:19
  • @EJP Nysteries are the spice of life! (lol) I just mean the JNI methods won't be 1:1 with OS functions, probably they'll be simpler. – Adriano Repetti Apr 10 '12 at 09:45
0

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.

Trey
  • 11,032
  • 1
  • 23
  • 21