What is the least-overhead way in Java 8+ to poll/read a small plain ASCII file to check if it changed?
I have a device that creates a fake filesystem (ev3dev) that provides small read-only files with status updates. (eg. a lego motor's position in a position
file with a single Int, or the motor's status in a status
file with a single String, both served up as a plain ASCII file)
Things that didn't work:
- Java's Watcher service doesn't work, I think because the OS is never notified that the file changed. (the modified time stays the same, and the file size is constant.) I tried -- it never triggers. Oh well!
- Setting a
mark()
andreset()
to read the file over and over without creating a new Reader doesn't seem to work on a BufferedReader, are there ways to make it work?
I think I have to poll: quickly read the file over and over (and over. and over!)
- Can I use a memory-mapped file (will it pick up the changes?)
- Some way to keep everything open and do a mark/reset or similar to pick up changes?
- Or just bite the bullet and call Files.readAllBytes in its own thread with a 1ms delay between loops?