<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## [路徑監聽](https://lingcoder.gitee.io/onjava8/#/book/17-Files?id=%e8%b7%af%e5%be%84%e7%9b%91%e5%90%ac) 通過**WatchService**可以設置一個進程對目錄中的更改做出響應。在這個例子中,**delTxtFiles()**作為一個單獨的任務執行,該任務將遍歷整個目錄并刪除以**.txt**結尾的所有文件,**WatchService**會對文件刪除操作做出反應: ~~~ // files/PathWatcher.java // {ExcludeFromGradle} import java.io.IOException; import java.nio.file.*; import static java.nio.file.StandardWatchEventKinds.*; import java.util.concurrent.*; public class PathWatcher { static Path test = Paths.get("test"); static void delTxtFiles() { try { Files.walk(test) .filter(f -> f.toString() .endsWith(".txt")) .forEach(f -> { try { System.out.println("deleting " + f); Files.delete(f); } catch(IOException e) { throw new RuntimeException(e); } }); } catch(IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { Directories.refreshTestDir(); Directories.populateTestDir(); Files.createFile(test.resolve("Hello.txt")); WatchService watcher = FileSystems.getDefault().newWatchService(); test.register(watcher, ENTRY_DELETE); Executors.newSingleThreadScheduledExecutor() .schedule(PathWatcher::delTxtFiles, 250, TimeUnit.MILLISECONDS); WatchKey key = watcher.take(); for(WatchEvent evt : key.pollEvents()) { System.out.println("evt.context(): " + evt.context() + "\nevt.count(): " + evt.count() + "\nevt.kind(): " + evt.kind()); System.exit(0); } } } /* Output: deleting test\bag\foo\bar\baz\File.txt deleting test\bar\baz\bag\foo\File.txt deleting test\baz\bag\foo\bar\File.txt deleting test\foo\bar\baz\bag\File.txt deleting test\Hello.txt evt.context(): Hello.txt evt.count(): 1 evt.kind(): ENTRY_DELETE */ ~~~ **delTxtFiles()**中的**try**代碼塊看起來有些多余,因為它們捕獲的是同一種類型的異常,外部的**try**語句似乎已經足夠了。然而出于某種原因,Java 要求兩者都必須存在(這也可能是一個 bug)。還要注意的是在**filter()**中,我們必須顯式地使用**f.toString()**轉為字符串,否則我們調用**endsWith()**將會與整個**Path**對象進行比較,而不是路徑名稱字符串的一部分進行比較。 一旦我們從**FileSystem**中得到了**WatchService**對象,我們將其注冊到**test**路徑以及我們感興趣的項目的變量參數列表中,可以選擇**ENTRY\_CREATE**,**ENTRY\_DELETE**或**ENTRY\_MODIFY**(其中創建和刪除不屬于修改)。 因為接下來對**watcher.take()**的調用會在發生某些事情之前停止所有操作,所以我們希望**deltxtfiles()**能夠并行運行以便生成我們感興趣的事件。為了實現這個目的,我通過調用**Executors.newSingleThreadScheduledExecutor()**產生一個**ScheduledExecutorService**對象,然后調用**schedule()**方法傳遞所需函數的方法引用,并且設置在運行之前應該等待的時間。 此時,**watcher.take()**將等待并阻塞在這里。當目標事件發生時,會返回一個包含**WatchEvent**的**Watchkey**對象。展示的這三種方法是能對**WatchEvent**執行的全部操作。 查看輸出的具體內容。即使我們正在刪除以**.txt**結尾的文件,在**Hello.txt**被刪除之前,**WatchService**也不會被觸發。你可能認為,如果說"監視這個目錄",自然會包含整個目錄和下面子目錄,但實際上:只會監視給定的目錄,而不是下面的所有內容。如果需要監視整個樹目錄,必須在整個樹的每個子目錄上放置一個**Watchservice**。 ~~~ // files/TreeWatcher.java // {ExcludeFromGradle} import java.io.IOException; import java.nio.file.*; import static java.nio.file.StandardWatchEventKinds.*; import java.util.concurrent.*; public class TreeWatcher { static void watchDir(Path dir) { try { WatchService watcher = FileSystems.getDefault().newWatchService(); dir.register(watcher, ENTRY_DELETE); Executors.newSingleThreadExecutor().submit(() -> { try { WatchKey key = watcher.take(); for(WatchEvent evt : key.pollEvents()) { System.out.println( "evt.context(): " + evt.context() + "\nevt.count(): " + evt.count() + "\nevt.kind(): " + evt.kind()); System.exit(0); } } catch(InterruptedException e) { return; } }); } catch(IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { Directories.refreshTestDir(); Directories.populateTestDir(); Files.walk(Paths.get("test")) .filter(Files::isDirectory) .forEach(TreeWatcher::watchDir); PathWatcher.delTxtFiles(); } } /* Output: deleting test\bag\foo\bar\baz\File.txt deleting test\bar\baz\bag\foo\File.txt evt.context(): File.txt evt.count(): 1 evt.kind(): ENTRY_DELETE */ ~~~ 在**watchDir()**方法中給**WatchSevice**提供參數**ENTRY\_DELETE**,并啟動一個獨立的線程來監視該**Watchservice**。這里我們沒有使用**schedule()**進行啟動,而是使用**submit()**啟動線程。我們遍歷整個目錄樹,并將**watchDir()**應用于每個子目錄。現在,當我們運行**deltxtfiles()**時,其中一個**Watchservice**會檢測到每一次文件刪除。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看