Hi Jathrek,
Thats sort of one idea I was thinking about or may be to find out the lenght of the file and scroll backwards until a new line character is reached - but I did not like this.
What I am doing is storing 4 elements of an array as a string separated by commas in the file, but I only want the last 4 elements in the array. What I am tempted to do is read each line out and populate the array, then read in the next line and overwrite the previous values, then when it hits the last line, it wont having anything to overwrite the values with, so leaving only the last values.
Although its only a small file, I think this is a rather ugly and possibly unreliable solution, but may get the job done, I was just checking to see if there was nothing I was missing!!!
Thanks again for your help
Cheers
john
Main Topics
Browse All Topics





by: JathrekPosted on 2007-04-25 at 12:58:12ID: 18976760
I don't think there's a real way to do it directly using J2SE classes (directly accessing the last line, I mean).
Maybe you could use some kind of "hack" using RandomAccessFile;
This class gives you a "seek(n)" method, which moves your pointer to the nTh character in the file.
So, if you've some idea of the average length of a line in your file (like 200 characters), you can just go to the (fileLengthInCharacter - 200)Th character of the file and read the 200 next characters to check if there's a break line (there's a "length()" method too that returns the number of bytes in the file, you could use it to reach the end of the file).
If there's, make sure it's the last one (there could be one ten character further) and then you can just read the following characters which will then be the last line.
If there's none, just go to the (fileLengthCharacter - 400)Th character and look for a break line in the 200 next characters (and go on by step of 200 characters).
Even though it's not an official solution, I hope it'll help you.