Search code examples
javafile-ionewlinefileinputstreamrandom-access

Cant find the newline character in a file (java)


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


Solution

  • It is just because you are using readline. It returns you the line String without the newline character whatever it was (CR/LF/CRLF).

    The Javadoc or RandomAccessFile#readLine() says:

    A line of text is terminated by a carriage-return character ('\r'), a newline character ('\n'), a carriage-return character immediately followed by a newline character, or the end of the file. Line-terminating characters are discarded and are not included as part of the string returned.

    If you try to find the last line, you can read you file until its end.