4

There are many approaches, one could use create a Program and just call the Unix tail command, however the problem with this is that the process must be installed on a Unix machine with tail installed.

The other option is to just read the file, line by line until there are no more lines, sleep for a period of time and then try to read the next line and repeat.

The question is what/how do I go about polling a file which is actively being written to. AFAIK there is no way in Java to get async notifications when a file has been modified as this is too low level (something that comes from the OS). I am also aware of no libs which support this.

I was thinking a better approach may be to sleep/poll on the modified date of a file..?

Any solutions?

NightWolf
  • 7,694
  • 9
  • 74
  • 121

4 Answers4

5

If you can use Java 7 there are File Watchers and you could implement what you need.
File Notifications

For Java 6 (and previous) you would have to use Apache Commons VFS (File Monitor) lib to create your own file watcher.

If you can't go to Java 7 or use third party libs (e.g commons or jnotify) you would have to write a file watchdog your self using polling threads.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • I suspect File Notifications is only for adding and removing entries. There is an ENTRY_MODIFY but I thought it was only changes to the directory entry rather than file length. – Peter Lawrey Apr 12 '12 at 07:22
3

You can use java nio's WatchService to get asynchronous notifications if a file/directory has changed. This tutorial explains the way to use it.

saravanan07
  • 365
  • 2
  • 4
  • 14
1

What you can do to simplify this is to create a Thread which poll the size of the file and only reads that much i.e. it never reaches the end as such. This data read can be written to a pipe allowing your main thread to read a continous pipe.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

I don't know answer. Just two tips
1. Maybe RandomAccessFile would help you.
2. Try find another open source program, which do this. (e.g. IntelliJ IDEA do exactly this)

bugs_
  • 3,544
  • 4
  • 34
  • 39