简体   繁体   English

如何判断文件是否已重命名?

[英]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. 该程序使用FileInputStream来继续读取文件。 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看起来是您问题的完美解决方案:

JNotify is a java library that allow java application to listen to file system events, such as: JNotify是一个java库,允许java应用程序监听文件系统事件,例如:

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. 我能想到的最简单的方法就是每5秒轮询一次文件大小或类似的东西。

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. 然后,您可以相应地重新创建FileinputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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