tgr
tgr

Reputation: 3608

How to track, if a File has been renamed?


I am continously reading a Logfile in realtime with a BufferedReader. This works pretty good. But every day there is a Log-rotate, which means my Logfile will be renamed and doesn't change anymore. A new Logfile will be created in the same place. Before I can change to that new Logfile, I need to finish reading the current one, which is no problem using BufferedReader, but how can I spot the filename has changed?

I tried calling file.getPath() and file.getAbsolutePath(), but this doesn't work, because the File-objects path is fixed after creation.

I also tried using the WatchService, but appending to the file causes a ENTRY_DELETE too. The resulting overead for checking if it was a rename is just to much.

Does anybody know a way to tell if the Logfile has been renamed?

Upvotes: 2

Views: 559

Answers (1)

Big Bad Baerni
Big Bad Baerni

Reputation: 996

You might try JNotify. Does the job, though don't know of the resources it takes.

Upvotes: 2

Related Questions