"Listen" directory using Java

Asked

Viewed 650 times

3

I am working on an application, and I need to create a functionality, as a method, that is "listening", given directory. The directory is used as a destination for *.xlsx file uploads.

I need that whenever there is a new file, my application read the file and do the processing. And I don’t have much idea and need help to make my application 'listening' the directory to do this processing whenever an upload is made.

1 answer

5


You can use the Watch Service API. See an example of how to do:

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

Path dir = ...;
try {
    WatchKey key = dir.register(watcher,
                           ENTRY_CREATE,
                           ENTRY_DELETE,
                           ENTRY_MODIFY);
} catch (IOException x) {
    System.err.println(x);
}

More details on http://docs.oracle.com/javase/tutorial/essential/io/notification.html

Other option

You can also use the Jnotify. Perhaps the implementation is a little more "simple". See an example in: https://stackoverflow.com/questions/4941869/directory-listener-in-java

  • Thanks @Anmaia, I believe this helps me, I will test do both implementations.

  • I had to perform a similar activity and it worked normally, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.