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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
package aide.forum.developpez;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileWatcher {
public static void main(String[] args) throws InterruptedException, IOException {
// make a new watch service that we can register interest in
// directories and files with.
// start the file watcher thread below
MyWatchQueueReader fileWatcher = new MyWatchQueueReader(
Paths.get("C:/devel/workspace/developpez/test"),
Paths.get("C:/devel/workspace/developpez/backup"));
Thread th = new Thread(fileWatcher, "FileWatcher");
th.start();
}
/**
* This Runnable is used to constantly attempt to take from the watch queue,
* and will receive all events that are registered with the fileWatcher it
* is associated. In this sample for simplicity we just output the kind of
* event and name of the file affected to standard out.
*/
private static class MyWatchQueueReader implements Runnable {
/**
* The format use to timestamp backupfiles
*/
static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
/** the watchService that is passed in from above */
private WatchService myWatcher;
/**
* Path to watch for backup to do
*/
private Path towatch;
/**
* Path where put backup
*/
private Path backup;
public MyWatchQueueReader(Path toWatch, Path backup) throws IOException {
this.towatch = toWatch;
this.backup = backup;
this.myWatcher = toWatch.getFileSystem().newWatchService();
toWatch.register(myWatcher, ENTRY_CREATE, ENTRY_MODIFY);
}
/**
* In order to implement a file watcher, we loop forever ensuring
* requesting to take the next item from the file watchers queue.
*/
@Override
public void run() {
try {
// get the first event before looping
WatchKey key = myWatcher.take();
while (key != null) {
// we have a polled event, now we traverse it and
// receive all the states from it
for (WatchEvent event : key.pollEvents()) {
System.out.printf("Received %s event for file: %s\n", event.kind(), event.context());
createBackupFile(event);
}
key.reset();
key = myWatcher.take();
}
} catch (
InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Stopping thread");
}
/**
* Create a backup of the modified file
* @param key
*
* @param event
*/
private void createBackupFile(WatchEvent event) {
String fileModifed = (String) event.context().toString();
if (fileModifed != null) {
Path child = Paths.get(towatch.toString(), fileModifed);
// We dont backup directory
if ( !Files.isDirectory(child)) {
Path backupFile = Paths.get(backup.toString(), format.format(new Date()) + fileModifed);
if (Files.exists(child) && !Files.exists(backupFile)) {
try {
Files.copy(child, backupFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
} |