Как настроить WatchService (java)
Как настроить WatchService, чтобы можно было следить за изменениями в какой-то другой папке, а не только в user.home или user.dir?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.watchservice;
import java.io.IOException;
import java.nio.file.*;
public class DirectoryWatcherExample {
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get(System.getProperty("user.dir"));
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
}
key.reset();
}
watchService.close();
}
}
Ответы (1 шт):
Автор решения: Evendie
→ Ссылка
Нужно изменить это:
Path path = Paths.get(System.getProperty("user.dir"));
На это:
Path path = Paths.get("Ваша_желаемая_директория");