Voici le code permettant d'écouter un répertoire (merci à tchize_ )
Ils me reste 2 petits problèmes néanmoins :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
32
33
34
35
36
37
38
39
40
41
42
43 import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; public class Test { public static void main(String args[]) throws InterruptedException { try { FileSystem fs = FileSystems.getDefault(); WatchService ws = null; try { ws = fs.newWatchService(); } catch (IOException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } Path path = fs.getPath("D:\\"); path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW, StandardWatchEventKinds.ENTRY_DELETE); WatchKey k = ws.take(); for (WatchEvent object : k.pollEvents()) { if (object.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { System.out.println("Modify: " + object.context().toString()); } if (object.kind() == StandardWatchEventKinds.ENTRY_DELETE) { System.out.println("Delete: " + object.context().toString()); } if (object.kind() == StandardWatchEventKinds.ENTRY_CREATE) { System.out.println("Created: " + object.context().toString()); } } } catch (IOException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } } }
- ce code n'écoute pas les sous répertoires de D:/ (si je créer un fichier dans D:/mondossier/monfichier.txt, il ne le vois pas)
- dès qu'il detecte un ajout/suppression/modification, le programme s'arrête. Idéalement il devrait tourner indéfiniement...
J'ai sûrement omis un détail ?
Partager