Skip to content

Instantly share code, notes, and snippets.

@lolson
Created April 19, 2016 21:53
Show Gist options
  • Save lolson/5bffb17b16bac0c9315814762f3a6ec8 to your computer and use it in GitHub Desktop.
Save lolson/5bffb17b16bac0c9315814762f3a6ec8 to your computer and use it in GitHub Desktop.
A java implementation of tail
// See JLogTailer
public void tailLog(File file) {
boolean running = true;
int updateInterval = 1000;
long filePointer = file.length();
try {
while (running) {
Thread.sleep(updateInterval);
long length = file.length();
if (length < filePointer) {
print("Log file was reset. Restarting logging from start of file.");
filePointer = length;
} else if (length > filePointer) {
RandomAccessFile localRandomAccessFile = new RandomAccessFile(file, "r");
localRandomAccessFile.seek(filePointer);
String str = null;
while ((str = localRandomAccessFile.readLine()) != null) {
// TODO add log cache logic
print(str);
}
filePointer = localRandomAccessFile.getFilePointer();
localRandomAccessFile.close();
}
}
} catch (Exception ex) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment