Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created July 8, 2014 11:29
Show Gist options
  • Save timpulver/0957e0c2c4da5b575727 to your computer and use it in GitHub Desktop.
Save timpulver/0957e0c2c4da5b575727 to your computer and use it in GitHub Desktop.
[Processing] Watch Data Folder for Changed / Newly Created Files
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
void setup(){
thread("watchDataPath");
}
void draw(){}
void fileChanged(String filename){
println("File Changed: " + filename);
// Do something here
}
void fileCreated(String filename){
println("File Created: " + filename);
// Do something here
}
/*
* Watches the data directory for newly created and modified files
* Make sure the data directory exists.
* It may take a few seconds until we get notified of changes here...
*/
public void watchDataPath() {
Path path = Paths.get(dataPath(""));
try {
// Check if path is a folder
Boolean isFolder = (Boolean) Files.getAttribute(path,
"basic:isDirectory", NOFOLLOW_LINKS);
if (!isFolder) {
throw new IllegalArgumentException("Path: " + path + " is not a folder");
}
} catch (IOException ioe) {
ioe.printStackTrace(); // Folder does not exist
}
System.out.println("Watching folder: " + path);
FileSystem fs = path.getFileSystem (); // We obtain the file system of the Path
// We create the new WatchService
try{
WatchService service = fs.newWatchService();
// We register the path to the service
path.register(service, ENTRY_CREATE); // We watch for new files
path.register(service, ENTRY_MODIFY); // and modified files
// Start the infinite polling loop
WatchKey key = null;
while(true) {
key = service.take();
// Dequeueing events
Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
// Get the type of the event
kind = watchEvent.kind();
if (OVERFLOW == kind) {
continue; //loop
} else if (ENTRY_CREATE == kind) {
Path newPath = ((WatchEvent<Path>) watchEvent).context(); // A new Path was created
fileCreated(newPath.toString());
} else if (ENTRY_MODIFY == kind) {
Path modifiedPath = ((WatchEvent<Path>) watchEvent).context(); // File / Folder modified
fileChanged(modifiedPath.toString());
}
}
if(!key.reset()) {
break; //loop
}
}
}catch(Exception e) {e.printStackTrace();}
}
// tags: Processing, P5, File, Files, Folder, Directory, Change, Changed, Create, Created, Modified, Edited, File System, Watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment