Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchService for Java 6 [closed]

Java 7 introduced WatchService for monitoring file systems continuously. Is there a backport for Java 6 ?

Are there pure Java libraries with similar features ?

like image 510
paradigmatic Avatar asked Nov 01 '11 15:11

paradigmatic


2 Answers

yes, of course. Apache VFS does exactly this. you can find it under http://commons.apache.org/vfs/. It's a pure java library that can monitor files and it's pretty easy to use:

FileSystemManager manager = VFS.getManager();
FileObject file= manager.resolveFile("c:/MyFile.txt");

DefaultFileMonitor fm = new DefaultFileMonitor(new MyListener());
fm.setDelay(5000);
fm.addFile(file); 
fm.start();

the code above will monitor the file c:/MyFile.txt. if it changes, the object new MyListener() is called.

like image 191
Thomas Uhrig Avatar answered Nov 15 '22 15:11

Thomas Uhrig


A pure Java library for this is impossible; you need a native component if you want to avoid polling.

http://wiki.netbeans.org/NativeFileNotifications gives some information about both the available native APIs and various Java libraries wrapping them.

like image 21
Jesse Glick Avatar answered Nov 15 '22 15:11

Jesse Glick