aspose file tools*
The moose likes I/O and Streams and the fly likes Handling File Rollover Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Handling File Rollover" Watch "Handling File Rollover" New topic
Author

Handling File Rollover

Freeman Fridie
Greenhorn

Joined: Nov 25, 2004
Posts: 10
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

Joined: Aug 29, 2005
Posts: 74
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

Joined: Nov 25, 2004
Posts: 10
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

Joined: Aug 29, 2005
Posts: 74
Try this:

Freeman Fridie
Greenhorn

Joined: Nov 25, 2004
Posts: 10
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

Joined: Aug 29, 2005
Posts: 74
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

Joined: Nov 25, 2004
Posts: 10
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Choon-Chern Lim
Ranch Hand

Joined: Aug 29, 2005
Posts: 74
Great explanation, Ernest. For some reasons, I have tough time explaining the things what I wrote.
Freeman Fridie
Greenhorn

Joined: Nov 25, 2004
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling File Rollover