Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling File Rollover

 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to simulate the unix tail command in java. I found the following code which does what I need:

BufferedReader IN = new BufferedReader(new FileReader("errors"));
EXECUTE = true;
String line;
while(EXECUTE){
line = IN.readLine();
if(line != null){
//do something with the text......
}
else{
try{
Thread.sleep(500);
} catch(Throwable t){}
}
}

The issue is that once per day the file errors gets renamed to something else and replaced with a new errors file. This rollover can happen at any time of the day.

How can I handle the rollover situation cleanly in the code. Right now the code does not detect when there is new errors file, it just returns null forever. I dont get a file not found exception or anything like that.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you mentioned that the file "errors" gets renamed to something else, is there a way for you to know the new file name?

The reason you are not getting filenotfound exception is because your "in" has a hardcoded reference to "errors" file, and it is only referenced once. Your "line" in your infinite while loop will eventually hit null and it will always fall into the else clause(sleep mode).
 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...

To try and clarify. What happens is the errors file get copied to another name and the replaced with a new empty errors file. The file name errors never actually changes.

Seems like the code keeps the "handle" to the old errors file and does not realize there is a new file (with the same name) that it needs to process.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works ....thanks!!!...Although I dont quite understand why it works. If you could give me a brief explanation that would be great.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure... first of all, you always need to reference the "errors" file. The reason for that is to allow you can obtain the "very" last modified datetime stamp when this file has been updated. If you reference this file only once, then your last modified date may not be the very last modified date.

Basically, in this code, I check if the last modified date is greater than the one previously stored. If it is, then we know that the "errors" file has been updated and therefore, we should read it (or do something with it).

It is also highly recommended to close the bufferedreader object once created each time you open the file.
 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excellent...Im almost there...how does this handle the daily roation of the file? It does. Just not clear to me how?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you rename a file on UNIX, any open file handles continue to work, and they continue to point to the same file. So in your original version, the single FileReader would end up reading from the renamed file, which never grew.

In the new version, when the end of file is reached, the file handle is closed and a new one opened. At this point, if the old file has been renamed and a new one has appeared, the new FileReader will point to the new file, and the program won't look at the old log file anymore.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation, Ernest. For some reasons, I have tough time explaining the things what I wrote.
 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation!...thanks. One more case that Im wondering if there is way to handle.

When the file rotates there could be a case where there is data in the rotated errors file that is not in the new errors file. Is it possible to code this in such a way so that I can do one last read on the rotated file to make sure I dont lose any transactions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic