views:

114

answers:

3

I have a folder in which continuously new files are being dumped.In Java,what is the best way to detect changes in file-system (ie. a specified folder in which the files are being dumped) and add the newly arrived files to a queue data structure so that i can sequentially process each incoming file.

I'm aware of listFiles() function in the File class but using this I can only get files that are available at an instant of time. Of course I can continuously poll the folder and get the list of files in it using a thread.But is this the best way or is there a better way to accomplish this.

+2  A: 

There is unfortunately no standard way to do this until JDK7 comes out. But there are some libraries available on the internet which use the native functions of different operating systems to do this.

The libraries which I have looked at are jPoller and jNotify But in the end I ended simply polling the directory which was interesting for me when I had to do that.

Jaka
+3  A: 

You can look for Apache Commons JCI FAM (FileAlterationMonitor)

DixonD
+5  A: 

Continuously polling is the way to do it in Java as of now - though don't poll too often, it can be quite a heavy operation if the directory contains lots of entries.

JDK 7 will have a specific API for doing just this java.nio.file.WatchFile

nos
+1 for WatchFile
Noel M
You could have linked to the JavaDoc instaed of a stupid google search.
Sylar