Is there any method to read a specific line from a text file ? In the API or Apache Commons. Something like :
String readLine(File file, int lineNumber)
I agree it's trivial to implement, but it's not very efficient specially if the file is very big.
If the lines you were reading were all the same length, then a calculation might be useful.
But in the situation when the lines are different lengths, I don't think there's an alternative to reading them one at a time until the line count is correct.
Using File Utils:
guava has something similar:
So you can do
Unfortunately, unless you can guarantee that every line in the file is the exact same length, you're going to have to read through the whole file, or at least up to the line you're after.
The only way you can count the lines is to look for the new line characters in the file, and this means you're going to have to read each byte.
It will be possible to optimise your code to make it neat and readable, but underneath you'll always be reading the whole file.
If you're going to reading the same file over and over again you could parse the file and create an index storing the offsets of certain line numbers, for example the byte count of where lines 100, 200 and so on are.
Not that I'm aware of.
Be aware that there's no particular indexing on files as to where the line starts, so any utility method would be exactly as efficient as:
(with appropriate error-handling and resource-closing logic, of course).
would do, but it still has the efficiency problem.
Alternatively, you can use:
This will be slightly more efficient due to the buffer.
Take a look at
Scanner.skip(..)
and attempt skipping whole lines (with regex). I can't tell if it will be more efficient - benchmark it.P.S. with efficiency I mean memory efficiency