WatchService for Java 6

paradigmatic picture paradigmatic · Nov 1, 2011 · Viewed 13.6k times · Source

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

Are there pure Java libraries with similar features ?

Answer

Thomas Uhrig picture Thomas Uhrig · Nov 2, 2011

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.