Skip to content

Instantly share code, notes, and snippets.

@sedj601
Created August 22, 2024 20:37
Show Gist options
  • Save sedj601/41237e0d1597df65037c3f8199fc73ae to your computer and use it in GitHub Desktop.
Save sedj601/41237e0d1597df65037c3f8199fc73ae to your computer and use it in GitHub Desktop.
JavaFX WatchService - Alerts When Changes Inside a Test Folder - This is probably full off issues!!!
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Path currentRelativePath = Paths.get("");
new File(currentRelativePath.toAbsolutePath().toString() + "/WatchServiceTestArea").mkdir();
WatchServiceHandler watchServiceHandler = new WatchServiceHandler(currentRelativePath.toAbsolutePath().toString() + "/WatchServiceTestArea");
Scene scene = new Scene(new StackPane());
primaryStage.setTitle("Calendar");
primaryStage.setScene(scene);
primaryStage.setWidth(1300);
primaryStage.setHeight(1000);
primaryStage.centerOnScreen();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Task;
/**
*
* @author blj0011
*/
public class WatchServiceHandler
{
StringProperty folderPath = new SimpleStringProperty();
WatchService watchService;
public WatchServiceHandler(String folderPath)
{
this.folderPath.addListener((obs, oldPath, newPath) -> {
System.out.println("Watching: " + newPath);
try
{
if(watchService == null)
{
watchService = FileSystems.getDefault().newWatchService();
}
else
{
watchService.close();
watchService = FileSystems.getDefault().newWatchService();
}
Path directoryPath = Paths.get(this.folderPath.get());
// Register the directory for specific events
directoryPath.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
Task<Void> watchServiceTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
// Infinite loop to continuously watch for events
while (true) {
WatchKey key = watchService.take();
for (WatchEvent<?> event : key.pollEvents())
{
// Handle the specific event
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE)
{
System.out.println("File created: " + event.context());
}
else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE)
{
System.out.println("File deleted: " + event.context());
}
else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY)
{
System.out.println("File modified: " + event.context());
}
}
// To receive further events, reset the key
key.reset();
Thread.sleep(500);
}
}
};
Thread thread = new Thread(watchServiceTask);
thread.setDaemon(true);
thread.start();
}
catch (IOException ex)
{
System.out.println(ex.toString());
}
});
this.folderPath.set(folderPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment