简体   繁体   中英

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 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?

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

也许您可以根据自己的需要观看文件系统并进行更改。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM