无法在文件中找到换行符(java)

发布于 2024-12-06 04:23:30 字数 1514 浏览 0 评论 0原文

我试图获取文件的最后一行,但我的输出显示它从未找到它。我还尝试寻找所有行都以“[”开头,但除非跳转完美,否则程序不会跳过“[”。我尝试寻找“\r\n”、System.getProperty(“line.separator”)、\r 和\n。这很可能是一个愚蠢的错误,但如果我有它但找不到它,那么其他人也可能会遇到它。

        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());
}

代码的想法来自于 快速读取文本文件的最后一行?

我使用的文件是这个 http://www.4shared.com/file/i4rXwEXz/file.html

感谢您的帮助,如果您发现我的代码有任何其他可以改进的地方,请告诉我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-12-13 04:23:30

这只是因为您使用的是readline
它返回您的行 String 没有换行符,无论它是什么(CR/LF/CRLF)。

Javadoc 或 RandomAccessFile#readLine() 说:

一行文本以回车符 ('\r')、换行符 ('\n')、紧跟换行符的回车符或文件结尾结束。行终止字符将被丢弃,并且不会作为返回字符串的一部分包含在内。

如果您尝试找到最后一行,则可以读取文件直到其末尾。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文