String line = FileUtils.readLines(file).get(lineNumber);
would do, but it still has the efficiency problem.
Alternatively, you can use:
LineIterator it = IOUtils.lineIterator(
new BufferedReader(new FileReader("file.txt")));
for (int lineNumber = 0; it.hasNext(); lineNumber++) {
String line = (String) it.next();
if (lineNumber == expectedLineNumber) {
return line;
}
}
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…