java7 ネットワーク越しにディレクトリ監視


macからwindowsの共有ディレクトリとwindowsからwindowsの共有ディレクトリは監視できた
linuxはインストールするのがめんどくさいからやめた
OSというか共有の仕方に関係ありそうな気もする

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;


public class JavaApplication1 {

    public static void main(String[] args) throws IOException, InterruptedException {
        FileSystem fileSystem = FileSystems.getDefault();
	// for mac -> windows
        //Path path = fileSystem.getPath("/Volumes/shared");
	// for windows -> windows
	//Path path = fileSystem.getPath("\\\\servername_or_ip\\dirname");
	Path path = fileSystem.getPath("/Volumes/shared");

        
        WatchService watchService = fileSystem.newWatchService();
        path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, 
                             StandardWatchEventKinds.ENTRY_CREATE,
                             StandardWatchEventKinds.ENTRY_DELETE);
        
        while(true) {
            WatchKey watchKey = watchService.take();
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println(event.kind() + " " + event.context());
            }
            watchKey.reset();
        }
    }
}