2014-01-23 175 views
0

我想寫一個基本的目錄監視器,打印一個文件是否被創建,更改或刪除。但是我無法顯示每個文件的'lastModified'時間。請參閱下面全碼:Java:WatchService:File.Lastmodified返回0

public static void main(String[] args) throws IOException, InterruptedException { 
    // TODO Auto-generated method stub 

    WatchService watcher = FileSystems.getDefault().newWatchService(); 

    Path dir = Paths.get("C:\\Users\\User1\\Desktop\\test"); 

    WatchKey key = dir.register(watcher, 

      StandardWatchEventKinds.ENTRY_CREATE, 
      StandardWatchEventKinds.ENTRY_DELETE, 
      StandardWatchEventKinds.ENTRY_MODIFY 
      ); 

    for (;;) { 


     WatchKey key2 = watcher.take(); 

     for (WatchEvent<?> event : key.pollEvents() ) { 

      WatchEvent.Kind<?> kind = event.kind(); 


      WatchEvent<Path> ev = (WatchEvent<Path>) event; 

      Path filename = ev.context(); 
      File fullFilename = filename.toFile(); 

      System.out.println("Event: |"+kind+"| Filename: "+fullFilename.getName()+"|Time: "+fullFilename.lastModified()); 


      if (fullFilename.exists()) { 


       System.out.println(fullFilename.getName()+" - Exists"); 

      } 

      else { 

       System.out.println("fullFileName does not exist"); 

      } 

     } 


     boolean valid = key.reset(); 
     if (!valid) { 
      break; 
     } 
    } 


} 

lastModified方法返回0。我已經嘗試了測試fileFullname對象是否實際存在,以及由於某種原因,它沒有。但是,當你使用fileFullname.getName()時,它返回文件名就好了。

我在做什麼錯?

我感謝您的幫助,並提前致謝!

回答

4

您需要根據所監視的目錄解析文件名。

因爲事件的背景下文件系統沒有你看着目錄defaultDirectory你不能得到lastModified值時,defaultDirectory是從應用程序運行的目錄,如果你檢查的fullFilename的存在,你會得到false

Path filename = ev.context(); 
File fullFilename = filename.toFile(); 
//fullFilename.exists(); returns false 

,所以你需要通過這種方式

Path name = (Path) event.context(); 
//dir is you watched directory 
File filename = dir.resolve(name).toFile(); 

完整的代碼解決它

import java.io.File; 
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.WatchKey; 
import java.nio.file.WatchService; 

public class Snippet { 
    public static void main(String[] args) throws IOException, InterruptedException { 

     Path pathToWatch = FileSystems.getDefault().getPath("C:\\tmp\\test"); 
     try (WatchService watchService = pathToWatch.getFileSystem().newWatchService()) { 
      Path dir = Paths.get("C:\\tmp\\test"); 
      dir.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); 
      WatchKey key = watchService.take(); 
      do { 
       for (final WatchEvent<?> event : key.pollEvents()) { 
        Path name = (Path) event.context(); 
        File filename = dir.resolve(name).toFile(); 
        System.out.println(dir + ": " + event.kind() + ": " + event.context() + ", Modified: " + filename.lastModified()); 
       } 
      } while (key.reset()); 
     } 
    } 
} 

更多信息在docs.oracle.com (Watching a Directory for Changes)

我希望這可以幫助你。

+0

這是有幫助的,並解決了我的問題。 非常感謝您的幫助! – user2728488