[ACCEPTED]-Reading a specific line from a text file in Java-apache-commons-io

Accepted answer
Score: 17
String line = FileUtils.readLines(file).get(lineNumber);

would do, but it still has the efficiency 6 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 5 will be slightly more efficient due to the 4 buffer.

Take a look at Scanner.skip(..) and attempt skipping 3 whole lines (with regex). I can't tell if 2 it will be more efficient - benchmark it.

P.S. with 1 efficiency I mean memory efficiency

Score: 5

Not that I'm aware of.

Be aware that there's 5 no particular indexing on files as to where 4 the line starts, so any utility method would 3 be exactly as efficient as:

BufferedReader r = new BufferedReader(new FileReader(file));
for (int i = 0; i < lineNumber - 1; i++)
return r.readLine();

(with appropriate 2 error-handling and resource-closing logic, of 1 course).

Score: 1

If the lines you were reading were all the 5 same length, then a calculation might be 4 useful.

But in the situation when the lines 3 are different lengths, I don't think there's 2 an alternative to reading them one at a 1 time until the line count is correct.

Score: 1

Unfortunately, unless you can guarantee 15 that every line in the file is the exact 14 same length, you're going to have to read 13 through the whole file, or at least up to 12 the line you're after.

The only way you can 11 count the lines is to look for the new line 10 characters in the file, and this means you're 9 going to have to read each byte.

It will 8 be possible to optimise your code to make 7 it neat and readable, but underneath you'll 6 always be reading the whole file.

If you're 5 going to reading the same file over and 4 over again you could parse the file and 3 create an index storing the offsets of certain 2 line numbers, for example the byte count 1 of where lines 100, 200 and so on are.

Score: 1

Because files are byte and not line orientated 6 - any general solutions complexity will 5 be O(n) at best with n being the files size 4 in bytes. You have to scan the whole file 3 and count the line delimiters until you 2 know which part of the file you want to 1 read.

Score: 1

guava has something similar:

List<String> Files.readLines(File file, Charset charset);

So you can do

String line = Files.readLines(file, Charsets.UTF_8).get(lineNumber);


Score: 1

Using File Utils:

File fileFeatures = new File(
String line = (String) FileUtils.readLines(fileFeatures).get(lineNumber);


Score: 0

If you are going to work with the same file 3 in the same way (looking for a text at certain 2 line) you can index your file. Line number 1 -> offset.

Score: 0

According to this answer, Java 8 enables us to 2 extract specific lines from a file. Examples 1 are provided in that answer.

More Related questions