After a lot of further googling, trial and error and more I came up with a solution that simply wraps RandomAccessFile
and exposes all methods. The readLine()
method however was much improved by talking the one from BufferedReader
with minor adjustments. Performance is now identical to it.
This so called class OptimizedRandomAccessFile
buffers readLine() calls as long as no other methods requiring or affecting the position in the file are called. eg in:
OptimizedRandomAccessFile raf = new OptimizedRandomAccessFile(filePath, "r");
String line = raf.readLine();
int nextByte = raf.read();
nextByte
will contain the first byte of the next line in the file.
The full code can be found on bitbucket.