티스토리 뷰

이전 글에서 폴더 모니터링을 봤으니 이번글에서는 파일을 받아 처리하는 부분을 살펴보자.

public class Monitor{
  private static Path sharedDirectoryPath;
  private static WatchKey watchKey;
  private static WatchService watchService;

  public static void main(String[] args){
    String path="D:\\test";
    sharedDirectoryPath = Paths.get(path);
    
    try{
         watchService = FileSystems.getDefault().newWatchService();

          watchKey = sharedDirectoryPath.register(watchService
          , StandardWatchEventKinds.ENTRY_CREATE
         , StandardWatchEventKinds.ENTRY_DELETE
         ,StandardWatchEventKinds.ENTRY_MODIFY);

}catch(IOException e){
  e.printStackTrace();
}

while(true){
  for(WatchEvent<?> watchEvent : watchKey.pollEvents()){
     System.out.println(watchEvent.context() + " " + watchEvent.kind());

if(watchEvent.kind() == StandardWatchEventKinds.ENTRY_CREATE){
   System.out.println("create");

    try{
        Thread.sleep(1);
        File file = new File(pathFile);
        FileReader filereader = new FileReader(file);
        BufferedReader br = new BufferedReader(filereader);
        String line = "";
        while((line=br.readLine()!=null){
           System.out.println(line);
        }
        br.close();

    }catch(IOException e){
        e.printStackTrace();
    }
}
}
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함