8

Using JDK 7 I've had success in watching specific directories for new file creations, deletions and modifications using java.nio.file.StandardWatchEventKinds.*

I'm hoping someone may know a way to get Java to detect new file creations regardless of their path.

I am wanting to do this so I can calculate an MD5 sum for each newly written file.

Thanks for any advice you can offer.

JonnyKash
  • 145
  • 6
  • Define "regardless of their path"? One filesystem? Any writable mount? Also, what operating system? (The short answer is "No" but there may be a caveat depending on the OS) – Brian Roach Jan 10 '12 at 00:12
  • Just Windows 7/NTFS for now. I'd like to monitor all mounted volumes. I'm testing JNotify as one possible solution. Thanks – JonnyKash Jan 10 '12 at 00:35

3 Answers3

0

One possibility - not a convenient one, but a possibility - is to walk the directory tree for the directories you want to watch, registering each in a WatchService. That's not a very nice way to go about it, and it could be a problem depending on how large the actual directory tree is.

Joseph Ottinger
  • 4,711
  • 1
  • 20
  • 23
0

Ok, short answer is I don't think Java can do that out of the box. You'd have to either intercept calls to the operating system which would require something closer to the bare metal, or you could do as suggested in another answer and register listeners to every folder from the root down, not to mention other drives in the case of windows machines.

The first approach would need custom JNI which assumes the OS has such a hook and allows user code access.

The second approach would work but could consume a large amount of memory to track all the listeners. In windows right-click on c:\ and select and see just how many folders we're talking about.

Community
  • 1
  • 1
Kelly S. French
  • 11,669
  • 9
  • 56
  • 90
0

I do not know StandardWatchEvents (although it sounds convenient).

One way to do one you want is to use a native window API such as ReadDirectoryChangesW (or volume changes). It's painful, but works (been there, done that, wish I had another option at the time).

ptyx
  • 3,872
  • 1
  • 17
  • 20