2

I was searching SO to see if there was a question about this but apparently not so. I am currently reading a text file using BufferedReader. My only problem is that I want to read the last line of my file ONLY. IE: I write something to a file 10 times, I want to get the 10th entry. I could easily just run a loop reading each line until I hit my 10th line, but i was wondering if there was a more efficient way since the file is going to keep on increasing and could end up being 100+ entries.

Also, I'd rather not use an external package if possible.

Thanks!

zamN
  • 236
  • 2
  • 7
  • 16
  • 2
    Don't optimize your code before you have proven it to be bottleneck. Do it as you describe how you could do it and see how well it works. – oherrala Nov 26 '10 at 20:30

4 Answers4

4

Reading 100 lines is hardly going to be a problem.

It is possible to read a text file backwards, but:

  • Depending on the encoding, it can be very hard. In particular, encodings which have variable widths for different characters, but don't make it obvious where the start of a character is
  • It's really, really ugly code even in good cases. You have to keep skipping backwards in the file, working out where the line starts, etc.

If you want to see just how horrible it is, I've got some code in this Stack Overflow question - it's in C#, but you should be able to understand it reasonably easily. Hopefully it's enough to convince you not to do it.

If you had a file with 100,000 lines it might be worth it... but 100? No way. Just read the whole file.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The biggest delay in reading a file is getting it off disk. For file less than 4K there is no difference in this time. For files less tha 64K, you won't notice the difference. – Peter Lawrey Nov 26 '10 at 21:02
3

Perhaps a file is the wrong tool. Where does the text file come from? How about inserting the lines into a database table? Then finding the last row is pretty easy and fast.

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
  • 1
    Although this doesn't answer the question, I simply couldn't agree more, so this deserves a vote. – BalusC Nov 26 '10 at 20:42
  • Thanks @BalusC. I don't mean to not answer the _question_ out of stubbornness or what have you, but instead offer another way to accomplish the _task_. Sometimes, I ask questions that are too specific. I am fortunate in that I have some geeky friends who are good at getting me to take a step back. – Tony Ennis Nov 26 '10 at 20:57
0

The only way to do this is if you knew the length of the file already, which it appears you don't. You'll just have to iterate over it. 100+ lines shouldn't take much longer to loop over than 10 lines. Now if you had a million lines, then you'd probably want to go to a database or something similar.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
0

The class you can use is FileChannel. The problem you will have (in any language) is you don't know where to position in the file to get the last line unless you know the exact length of the line. You can just back up enough to get any reasonable line length and then search forward for it.

JOTN
  • 6,120
  • 2
  • 26
  • 31