Reputation: 10298
I have a program monitors log file changes. The program uses a FileInputStream to keep reading from the file. In the mean while, the log file might be rotated: the old log file is renamed and new log entries are written into a new file.
How can my program determines the file was rename so that it switch to read on the new log file?
Upvotes: 3
Views: 1157
Reputation: 22292
According to this question, JNotify sseems to be the perfect solution to your problem :
JNotify is a java library that allow java application to listen to file system events, such as:
File created
File modified
File renamed
File deleted
Upvotes: 5
Reputation: 8550
The easiest way I can think of is simply to poll for the file size every 5 seconds or something like that.
If the file you are reading from was renamed and a new one was created, then your polling will show a sudden decrease in size. You can then recreate you FileinputStream accordingly.
Upvotes: 0