7

I need to know when a new file appears in a directory. Obviously, I could poll the file system periodically, but that has all the normal downsides of polling mechanisms.

I know that windows supports file system events, and this project is already constrained to the Windows platform by other requirements.

Does anyone have experience receiving Windows filesystem events inside a JVM? If so what are the best practices, patterns, and/or libraries you have used?

A quick google turns up this library. Does anyone have experience with it (or any other) that they'd be willing to share?

Jared
  • 25,520
  • 24
  • 79
  • 114
  • this question has at least 2 duplicated on stack overflow – dfa Jul 21 '09 at 18:58
  • possible dup of http://stackoverflow.com/questions/730186/suggestions-sample-code-for-filesystemwatcher-in-java/732418 – notnoop Jul 21 '09 at 18:59
  • another possible dup: http://stackoverflow.com/questions/1096404 – dfa Jul 21 '09 at 19:00
  • Thanks for the dupe links - I didn't find those with an (apparently too brief) search. I've voted to close, especially for 1096404. – Jared Jul 22 '09 at 15:42
  • possible duplicate of [Is there a sophisticated file system monitor for Java which is freeware or open source?](http://stackoverflow.com/questions/1096404/is-there-a-sophisticated-file-system-monitor-for-java-which-is-freeware-or-open-s) – Jared Jul 09 '11 at 19:30

6 Answers6

7

I think this is one of the key features of Java 7 when it is more available. Example code from Sun's blog on Java 7:

import static java.nio.file.StandardWatchEventKind.*;

Path dir = ...;
try {
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
} catch (IOException x) {
    System.err.println(x);
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Brian
  • 13,412
  • 10
  • 56
  • 82
2

For Java 6 or older use JNA's com.sun.jna.platform.win32.FileMonitor.

dB.
  • 4,700
  • 2
  • 46
  • 51
1

Something quite low level and OS-specific like that is going to need native code (as per the link you mention). That library looks relatively small, so in the worst case you could probably fix any problems if your C++ is good enough.

If you can at all avoid it though, I'd suggest not using native code - library or not. What is so demanding about your scenario that you can't have a background thread poll?

Draemon
  • 33,955
  • 16
  • 77
  • 104
1

Brian Agnew's recommendation of JNotify is a good one: Is there a sophisticated file system monitor for Java which is freeware or open source?

Description from http://jnotify.sourceforge.net/:

JNotify is a java library that allow java application to listen to file system events, such as:

  • File created
  • File modified
  • File renamed
  • File deleted
Community
  • 1
  • 1
Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
1

You can use opensource JPollar framework to poll the directory in time intervals. It also allows time based polling.

Ritesh
  • 11
  • 1
0

If not using Jdk7 you'll have to use JNI.

John Doe
  • 869
  • 5
  • 10