aleung
aleung

Reputation: 10298

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?

Upvotes: 3

Views: 1157

Answers (3)

Riduidel
Riduidel

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

Yahel
Yahel

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

npinti
npinti

Reputation: 52185

Maybe you can watch the file system and make changes according to your needs.

Upvotes: 0

Related Questions