You basically have to read the file backwards. The simplest approach, without using "block" reads, is to the get the length of the file, and then use RandomAccessFile to read bytes at (length--) until you have counted the required number of line feeds / carriage returns. You can then read the bytes forward for one line.
Something like this....
RandomAccessFile randomAccessFile = new RandomAccessFile("the.log", "r");
long offset = randomAccessFile.length() - 1;
int found = 0;
while (offset > 0 && found < 10) {
randomAccessFile.seek(offset--);
if (randomAccessFile.read() == 10) {
found++;
}
}
System.out.println(randomAccessFile.readLine());
Single byte reads many not be super efficient. If performance becomes a problem, you take the same approach, but read larger blocks of the file (say 8K) at a time, rather than 1 byte at a time.