Created
July 5, 2017 07:59
-
-
Save veeenu/c46d25415172e16d8ce738804f640a66 to your computer and use it in GitHub Desktop.
Simple Java file watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.nio.file.*; | |
import java.awt.event.*; | |
import java.util.*; | |
import java.util.function.*; | |
public class FsWatcher { | |
private Thread t; | |
private File file; | |
private Path dir; | |
private Consumer<File> listener; | |
private volatile boolean running; | |
public FsWatcher(String filename) { | |
file = new File(filename).getAbsoluteFile(); | |
dir = Paths.get(file.getParent()); | |
running = false; | |
} | |
public void setListener(Consumer<File> c) { | |
listener = c; | |
} | |
public void start() { | |
if(t != null) | |
stop(); | |
t = new Thread(() -> { | |
try(final WatchService ws = FsWatcher.this.dir.getFileSystem().newWatchService()) { | |
FsWatcher.this.dir.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); | |
while(FsWatcher.this.running) { | |
WatchKey wk = ws.take(); | |
List<WatchEvent<?>> events = wk.pollEvents(); | |
boolean isModified = false; | |
for(WatchEvent evt : events) { | |
File f = new File(evt.context().toString()); | |
if(file.getAbsolutePath().equals(f.getAbsolutePath())) { | |
isModified = true; | |
break; | |
} | |
} | |
wk.reset(); | |
wk.cancel(); | |
if(isModified) { | |
listener.accept(file); | |
} | |
FsWatcher.this.dir.register(ws, StandardWatchEventKinds.ENTRY_MODIFY); | |
} | |
} catch(InterruptedException e) { | |
// will happen if called by FsWatcher.stop() | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
}); | |
t.start(); | |
running = true; | |
} | |
public void stop() { | |
running = false; | |
if(t != null) { | |
t.interrupt(); | |
} | |
t = null; | |
} | |
public boolean isRunning() { | |
return running; | |
} | |
public static void main(String[] args) { | |
if(args.length < 1) { | |
System.out.println("Usage: java FsWatcher file"); | |
return; | |
} | |
FsWatcher fsw = new FsWatcher(args[0]); | |
fsw.setListener((f) -> { | |
System.out.println("File changed " + f); | |
try { | |
if(((File)f).delete()) { | |
BufferedWriter bw = new BufferedWriter(new FileWriter(f)); | |
bw.write("FrankerZ\nFrankerZ FrankerZ\nFrankerZ FrankerZ FrankerZ\nFrankerZ FrankerZ\nFrankerZ"); | |
bw.close(); | |
} | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
fsw.start(); | |
try { | |
Thread.sleep(1000); | |
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(args[0]))); | |
bw.write("super memes"); | |
bw.close(); | |
Thread.sleep(1000); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} finally { | |
fsw.stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment