package kr.purred.nioWatch.main;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.concurrent.TimeUnit;
public class Program
{
/**
* @param args
*/
public static void main (String[] args)
{
WatchService watchService = null;
Path path = null;
try
{
path = Paths.get ("P:/Dev/Java/Sample/NIO/nioWatch/src/kr/purred/nioWatch/main");
watchService = FileSystems.getDefault ().newWatchService ();
path.register (watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
while (!Thread.interrupted ())
{
// 와치 키를 가져오고 큐에서 키를 제거
WatchKey key = watchService.poll ((long) 1, TimeUnit.SECONDS);
if (key != null)
{
for (WatchEvent<?> watchEvent : key.pollEvents ())
{
// 미처리 이벤트 수
System.out.println ("WatchEvent Count : " + watchEvent.count ());
// 이벤트 종류를 가져온다.
Kind<?> kind = watchEvent.kind ();
// OVERFLOW 이벤트를 처리한다.
if (kind == StandardWatchEventKinds.OVERFLOW)
continue;
System.out.println (kind);
WatchEvent<Path> pathWatchEvent = (WatchEvent<Path>) watchEvent;
Path fileName = pathWatchEvent.context ();
System.out.println (fileName.getFileName ());
}
System.out.println ("키 Reset");
boolean vaildKey = key.reset ();
if (!vaildKey)
break;
}
else
{
System.out.println ("감시중 ...");
}
}
watchService.close ();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}