java7 RC 버그로 의심되는 코드가 있었다.


c:\test 라는 디렉토리를 계속 모니터링하고, 디렉토리안에서 특정 폴더나 파일이 변경되면 알려주는 간단한 코드이다.



import java.nio.channels.CompletionHandler;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import static java.nio.file.StandardWatchEventKinds.*;
public class WatchServiceTest {
 public static void main(String[] args) throws Exception {
  Path dir = Paths.get("c:\\test\\");
  WatchService watcher = FileSystems.getDefault().newWatchService();
  WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE,
    ENTRY_MODIFY);
  for (;;) {
   key = watcher.take();
   for (WatchEvent<?> event : key.pollEvents()) {
    if (event.kind() == ENTRY_CREATE) {
     Path name = (Path) event.context();
     System.out.println("path : " + name.toAbsolutePath());
     System.out.format("%s created%n", name);
    } else if (event.kind() == ENTRY_DELETE) {
     Path name = (Path) event.context();
     System.out.println("path : " + name.toAbsolutePath());
     System.out.format("%s deleted%n", name);
    } else if (event.kind() == ENTRY_MODIFY) {
     Path name = (Path) event.context();
     System.out.println("path : " + name.toAbsolutePath());
     System.out.format("%s modified%n", name);
    }
   }
   key.reset();
  }
 }
}


c:\test\ 에서 폴더를 생성했는데.. 잉??
workspace 에서 폴더를 생성했다고 나온다..

WatchEvent 클래스의 context 메소드를 호출하면 Path 가 나오는데, 내부적으로 꼬였나 보다.


path : E:\jdk1.7\test\새 폴더
새 폴더 created
path : E:\jdk1.7\test\새 폴더 (2)
새 폴더 (2) created
path : E:\jdk1.7\test\새 폴더 (2)
새 폴더 (2) deleted
path : E:\jdk1.7\test\make
make created



관련해서 Oracle BugDatabase에 신고했다.


Posted by '김용환'
,