あなたが使用することができNIO WatchService
:
の変更やイベントのために登録されたオブジェクトを時計ウォッチサービス。例えば、ファイルマネージャは、監視サービスを使用してディレクトリの変更を監視し、ファイルの作成または削除時にファイルリストの表示を更新することができます。
に必要な、それを使用するには:
// 1 create the watchService
WatchService watchService = FileSystems.getDefault().newWatchService();
// 2 get a reference to the directory to be watched for changes
String watchedDir = "/mydir";
Path dir = Paths.get(watchedDir);
// 3 register on the events you need to watch
WatchKey watchKey = dir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
...
// 4 wait for changes, generally inside a loop
watchKey = watchService.take();
時に利用できる方法テイクは、そうでない場合は待機を時計のキーを返します。