Note that there are some explanatory texts on larger screens.

plurals
  1. POCant find the newline character in a file (java)
    text
    copied!<p>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. </p> <pre><code> 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()); } </code></pre> <p>The idea from the code comes from <a href="https://stackoverflow.com/questions/686231/java-quickly-read-the-last-line-of-a-text-file">Quickly read the last line of a text file?</a></p> <p>The file I use is this <a href="http://www.4shared.com/file/i4rXwEXz/file.html" rel="nofollow noreferrer">http://www.4shared.com/file/i4rXwEXz/file.html</a></p> <p>Thanks for the help, and if you see any other places where my code could be improve let me know</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload