21

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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Lluis Martinez
  • 1,963
  • 8
  • 28
  • 42

9 Answers9

17
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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
5

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:

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

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

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
1

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.

pavium
  • 14,808
  • 4
  • 33
  • 50
  • And "same length" means same length in bytes, not characters (with variable length character encoding in mind) – MBO Jan 26 '10 at 09:21
  • Actually the input file is fixed length and ANSI, I forget to specify this in the question. The problem could be the line separator, the application must run both in Windows and Unix. – Lluis Martinez Jan 26 '10 at 10:18
1

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.

David Webb
  • 190,537
  • 57
  • 313
  • 299
1

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
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);
finnw
  • 47,861
  • 24
  • 143
  • 221
1

Using File Utils:

File fileFeatures = new File(
                "Homework1AdditionalFiles/jEdit4.3/jEdit4.3ListOfFeatureIDs.txt");
String line = (String) FileUtils.readLines(fileFeatures).get(lineNumber);
Milson
  • 1,525
  • 3
  • 15
  • 29
0

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

Mykola Golubyev
  • 57,943
  • 15
  • 89
  • 102
0

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

Community
  • 1
  • 1
João Matos
  • 6,102
  • 5
  • 41
  • 76