【監聽文件 多線程】使用java--WatchService監聽文件 開啟多線程copy文件


有一個小需求:

在PC跟前沒有人的時候,迅雷下載文件 至PC磁盤上,並且自動移動文件到U盤上,小主只要在走的時候取走U盤即可。

基於這個需求,有了下面這段代碼:【JDK  1.8】

 1 package com.sxd.moveFile;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.nio.file.Path;
 9 import java.nio.file.Paths;
10 import java.nio.file.StandardWatchEventKinds;
11 import java.nio.file.WatchKey;
12 import java.nio.file.WatchService;
13 import java.util.concurrent.TimeUnit;
14 
15 import org.junit.Test;
16 
17 import com.google.common.io.Files;
18 
19 /**
20  * 監聽某個文件夾下--如果有新的文件出現 則移動文件到指定目錄下
21  * @author Administrator
22  *
23  */
24 public class MoveFile implements Runnable{
25     
26     public static String fileName = null;
27     
28     @Test
29     public void test() throws IOException, InterruptedException{
30         moveFile();
31     }
32     
33     public void moveFile() throws IOException, InterruptedException{
34         
35         
36             final Path path = Paths.get("E:/迅雷下載");
37             final WatchService watchService = path.getFileSystem().newWatchService();
38             path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);//創建並啟動監聽 --創建文件的事件
39             
40 //            final WatchKey watchKey = watchService.poll(20, TimeUnit.MINUTES);//poll()監聽停止的條件--持續20分鍾 20分鍾后關閉監聽
41             final WatchKey  watchKey = watchService.take();//take()等不到此類事件發生就一直運行
42             
43             if(watchKey != null) {
44                 watchKey.pollEvents().stream().forEach(event ->
45                 {
46                     //執行本次 觸發后的方法體
47                     System.out.println(event.context());System.out.println(event.kind().name());
48                     //僅 判斷 新創建文件后綴為.rmvb的   才進行如下 操作
49                     if(event.context().toString().substring(event.context().toString().lastIndexOf(".")).equals(".rmvb")  || event.context().toString().substring(event.context().toString().lastIndexOf(".")).equals(".mkv")){
50                         fileName = event.context().toString();
51                         
52                         MoveFile mf = new MoveFile();
53                         Thread thread1 = new Thread(mf);//開啟一個新的線程 
54                         thread1.start();
55                     }
56                 });
57                 //再次執行
58                 try {
59                     moveFile();
60                 } catch (Exception e1) {
61                     e1.printStackTrace();
62                 }
63                 
64             }
65     
66     }
67 
68     @Override
69     public void run() {
70         try {
71             Files.copy(new File("E:/迅雷下載/"+fileName), new File("J:/U盤/迅雷下載/"+fileName));
72         } catch (Exception e) {
73             e.printStackTrace();
74         }
75     }
76 }
View Code

 

 

其中 關於文件系統注冊的監聽器上,有監聽事件類型:

Overflow包含了文件的創建/刪除/修改各種事件。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM