I am trying to get the last line of a file, but my output shows that it never finds it. I also tried looking for "[" which all the lines start with, but unless the jumps were perfect the program will not skip "[". I tried looking for "\r\n", System.getProperty("line.separator"), \r and \n. Most probably its a dumb mistake, but if I had it and I couldn't find it, then someone else may run into it too.
RandomAccessFile raf = new RandomAccessFile(fileLocation, "r");
//Finds the end of the file, and takes a little more
long lastSegment = raf.length() - 5;
//**Looks for the new line character \n?**
//if it cant find it then take 5 more and look again.
while(stringBuffer.lastIndexOf("\n") == -1) {
lastSegment = lastSegment-5;
raf.seek(lastSegment);
//Not sure this is the best way to do it
String seen = raf.readLine();
stringBuffer.append(seen);
}
//Trying to debug it and understand
int location = stringBuffer.lastIndexOf("\n");
System.out.println(location);
FileInputStream fis = new FileInputStream(fileLocation);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);
br.skip(lastSegment);
System.out.println("br.readLine() " + br.readLine());
}
The idea from the code comes from Quickly read the last line of a text file?
The file I use is this http://www.4shared.com/file/i4rXwEXz/file.html
Thanks for the help, and if you see any other places where my code could be improve let me know