How can I tell if a file was renamed?
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 m开发者_StackOverflow社区ight 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?
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
Maybe you can watch the file system and make changes according to your needs.
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.
精彩评论