"Listen" directory using Java

3

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

I need every time a new file exists, my application reads the file and processes it. And I do not have much idea and need help to make my application 'listening' to the directory to do this every time an upload is done.

    
asked by anonymous 16.07.2014 / 19:18

1 answer

5

You can use Watch Service API . Here's an example of how to do it:

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 at link

Another option

You can also use JNotify . Perhaps the implementation is a bit more "simple". See an example at: link

    
16.07.2014 / 19:29