Bonjour,

Explication: Je veux qu'il y est une class qui tourne tout le temps et qu'a chaque modification dans le répertoire, un fichier xml soit mis à jour.

La class pour créer le fic xml est faite et fonctionne
La class qui "Ecoute" le répertoir et tourne tout le temps fonctionne

Le problème est: Quand il y a modification d'un fic dans le rep,j'ai plusieurs fois le create modified et deleted qui se font

Sachant que pour une création ou suppression, une seul "creted ou deleted se fait se fait.

La Question: Comment faire pour que soit il y est une seul apparition quand il y a modif?
Les entiers sont juste ici pour m'aider à trouver la solution.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package Lecteur;
 
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
 
class MonWatcher extends Thread {
 
    public static void main(String[] args) throws IOException, InterruptedException {
        WatchService watchService
                = FileSystems.getDefault().newWatchService();
 
        Path chemin1 = Paths.get(System.getProperty("user.dir") + File.separator + "referentiel" + File.separator + "csv");
 
        Path path = Paths.get(String.valueOf(chemin1));
 
        path.register(
                watchService,
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_DELETE,
                StandardWatchEventKinds.ENTRY_MODIFY);
 
        WatchKey key;
 
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
 
        while ((key = watchService.take()) != null) {
 
            for (WatchEvent<?> event : key.pollEvents()) {
                System.out.println(
                        "Event kind:" + event.kind()
                                + ". File affected: " + event.context() + ".");
 
                if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) {
//                    System.out.println("1");
                    a++;
                }
                if (event.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)) {
//                    System.out.println("3");
                    c++;
                }
                if (event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
//                    System.out.println("2");
                    c++;
                    b++;
                    a++;
                }
//                **********************CREATION*************************
                if (a == 1 && b == 0 && c == 0) {
 
                    System.out.println("it's ok");
                    System.out.println("CREATED");
                    System.out.println("a=" + a + "b=" + b + "c=" + c);
                    c++;
//                    break;
 
                }
//                **********************DELETED*************************
                if (a == 0 && b == 0 && c == 1) {
                    System.out.println("it's ok");
                    System.out.println("DELETED");
                    System.out.println("a=" + a + "b=" + b + "c=" + c);
                    a++;
//                    break;
                }
//                if (a == 1 && b == 1 && c == 1) {
//                    System.out.println("ici mdrr");
//                }
//                System.out.println("a=" + a + "b=" + b + "c=" + c);
//
//                d++;
//            }
                System.out.println("a=" + a + "b=" + b + "c=" + c);
 
                System.out.println("d=" + d);
 
//                if (a == 1 && c==1) {
                    key.reset();
 
//                }
                a = b = c = 0;
                System.out.println("Fin for");
            }
 
        }
    }
}