2013-07-25 58 views
3

我已經做了一些代碼檢測改變目錄C:/ java/newfolder它工作良好。我在下面給出。看着一個目錄和子目錄進行創建,修改和更改java

import java.nio.file.*; 
import java.util.List; 

public class DirectoryWatchExample { 
public static void testForDirectoryChange(Path myDir){ 

     try { 
      WatchService watcher = myDir.getFileSystem().newWatchService(); 
      myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, 
      StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); 

      WatchKey watckKey = watcher.take(); 

      List<WatchEvent<?>> events = watckKey.pollEvents(); 
      for (WatchEvent event : events) { 
       if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { 
        System.out.println("Created: " + event.context().toString()); 
       } 
       if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { 
        System.out.println("Delete: " + event.context().toString()); 
       } 
       if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { 
        System.out.println("Modify: " + event.context().toString()); 
       } 
      } 

     } catch (Exception e) { 
      System.out.println("Error: " + e.toString()); 
     } 
} 

public static void main (String[] args) { 
    Path myDir = Paths.get("c:/java/newfolder/"); 
    //define a folder root 
    testForDirectoryChange(myDir); 

} 
} 

現在我只看目錄。但我需要只看全部子目錄。

爲前:c:/java/newfolder/folder1, c:/java/newfolder/folder2, c:/java/newfolder/folder3......etc

我上面子目錄給出的例子

C:/ JAVA/newfolder/* ..

我需要觀看所有子目錄給我一些解決方案

回答

0

我不熟悉的Path API,所以採取以鹽糧以下。

您正在註冊一個用於監控的目錄,並且每當它的一個直接後代被修改/創建/刪除時都會收到通知。

你需要做的第一件事是註冊所有的子目錄觀看:

// Used to filter out non-directory files. 
// This might need to filter '.' and '..' out, not sure whether they're returned. 
public class DirectoryFilter implements FileFilter { 
    public boolean accept(File file) { 
     return file.isDirectory(); 
    } 
} 


// Add this at the end of your testForDirectoryChange method 
for(File dir: myDir.toFile().listFiles(new DirectoryFilter())) { 
    testForDirectoryChange(dir.toPath()); 
} 

這將遞歸探索你的文件結構和登記每個目錄觀看。 請注意,如果您的目錄樹太深,遞歸可能不是一個可接受的解決方案,您可能需要「iterify」它。

您需要做的第二件事是,無論何時您收到目錄創建事件,都不要忘記註冊新的目錄進行監視。

無論如何我都會這麼做,但是手邊沒有有效的Java 1.7安裝,我無法測試它。請讓我知道它是否有效!

-1

那麼你想要的是列出目錄中的文件/子目錄? 您可以使用:

File dir = new File("c:/java/newfolder/"); 
File[] files = dir.listFiles(); 

listFiles()爲您提供了文件的目錄或爲null,它不是一個目錄。

然後,你可以這樣做:

for (File file: files){ 
    if (file.listFiles != null){ 
      //It's a subdirectory 
      File [] subdirFiles = file.listfiles; 
    } 
} 
+0

海路易斯不慣於列出files.i需要觀看子directory.give我一些解決方案.. – prasanth

+0

子目錄也在文件數組中。檢查我的編輯。 –

10

你想要做的是遞歸註冊WatchService並繼續在隨後的ENTRY_CREATE事件中註冊它。甲骨文提供了一個完整的示例:http://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java

通常我不會在單個鏈接上發表帖子,但是我懷疑甲骨文會採取相當基本的教程,答案太冗長了。以防萬一,通過搜索「java watchservice遞歸」很容易找到示例。

+0

我對此持懷疑態度,但它只是起作用。給它一個輸入目錄名稱,它也會監視所有的子目錄。這正是我想要的。感謝張貼的鏈接。 – Daniel

+0

我也是。它在檢測到他的創建時註冊新文件夾。如果某些條件(服務器負載,小文件),我認爲它可能會錯過在子文件夾中創建的文件。 – Nereis

+0

不好意思,但它的行爲就像Nereis暗示的一樣。 Oracle示例不具有確定性。嘗試讓它看一個空目錄,然後複製一箇中等大小的目錄樹。一些文件不會被注意到。 – Newerth

2

這是我來了..

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

    final SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>(){ 
     @Override 
     public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException 
     { 
      dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); 

      return FileVisitResult.CONTINUE; 
     } 
    }; 

    Files.walkFileTree(Paths.get("/directory/to/monitor"), fileVisitor);