I want to know when a file has rotated because because I'm watching a file and I have to get a new file with the content of this file and the new file. I have to use Java6, so I don't have the new features of JDK7.
The problem is that when I rotate the file (keeping a reference to the file), the old reference gets updated, I would like that it'd point to the old file.
I was doing something like this:
if (reader == null) {
reader = new RandomAccessFile(toWatch, "r");
}
System.out.println("LasModified:" + new Date(toWatch.lastModified()));
System.out.println("Cuando se creo to Watch long:" + reader.length()); --> lenght is okay.
//Here, I use logrotate to simulate what it could happen. (I execute the code in debug mode to be able to do it
long len = 0L;
File f = new File("/home/gortiz/logRotate/test.flume");
System.out.println("LasModified NewReader:" + new Date(f.lastModified()));
RandomAccessFile newReader = new RandomAccessFile(toWatch, "r");
len = reader.length(); --> len = 0
long newLength = newReader.length(); --> newLenght = 0
I could check if there's a file with name.1. Usually logs system renames logs like that.. but, it's not a good solution because nobody guarantees that that's going to be as log system renames files or even they could be moved to another directory.