Search code examples
javafileio

Quickly read the last line of a text file?


What's the quickest and most efficient way of reading the last line of text from a [very, very large] file in Java?


Solution

  • Have a look at my answer to a similar question for C#. The code would be quite similar, although the encoding support is somewhat different in Java.

    Basically it's not a terribly easy thing to do in general. As MSalter points out, UTF-8 does make it easy to spot \r or \n as the UTF-8 representation of those characters is just the same as ASCII, and those bytes won't occur in multi-byte character.

    So basically, take a buffer of (say) 2K, and progressively read backwards (skip to 2K before you were before, read the next 2K) checking for a line termination. Then skip to exactly the right place in the stream, create an InputStreamReader on the top, and a BufferedReader on top of that. Then just call BufferedReader.readLine().