Skip to content

Instantly share code, notes, and snippets.

@dangnhdev
Last active February 15, 2016 07:04
Show Gist options
  • Save dangnhdev/df9507d0837609cc314c to your computer and use it in GitHub Desktop.
Save dangnhdev/df9507d0837609cc314c to your computer and use it in GitHub Desktop.
Test WatchDirService
import com.activestudy.Utility.file.watchservice.WatchDirService;
import com.activestudy.Utility.file.watchservice.WatchEventAction;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
/**
*
* @author NguyenHaiDang
*/
public class TestASLib {
public static void main(String[] args) throws IOException, InterruptedException {
//tạo một object WatchDirService, theo dõi quá trình tạo file và xóa file
WatchDirService watchService = new WatchDirService(StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
//đăng ký thư mục cần theo dõi, có thể đăng ký nhiều lần
watchService.registerDir(Paths.get("E:\\"));
//đăng ký hành động khi có file mới được tạo
watchService.setOnCreatedAction(new WatchEventAction() {
@Override
public void doAction() {
//lấy file vừa được tạo
Path path = watchService.getCurrentFilePath();
//in ra màn hình đường dẫn tuyệt đối của file
System.out.println(path.toAbsolutePath().toString());
}
});
//đăng ký hành động khi có file vừa bị xóa
watchService.setOnDeletedAction(new WatchEventAction() {
@Override
public void doAction() {
//lấy file vừa được tạo
Path path = watchService.getCurrentFilePath();
//in ra màn hình đường dẫn tuyệt đối của file
System.out.println(path.toAbsolutePath().toString());
}
});
//khởi động watch service
watchService.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment