无法在文件中找到换行符(java)
我试图获取文件的最后一行,但我的输出显示它从未找到它。我还尝试寻找所有行都以“[”开头,但除非跳转完美,否则程序不会跳过“[”。我尝试寻找“\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是因为您使用的是
readline
。它返回您的行
String
没有换行符,无论它是什么(CR/LF/CRLF)。Javadoc 或 RandomAccessFile#readLine() 说:
如果您尝试找到最后一行,则可以读取文件直到其末尾。
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:
If you try to find the last line, you can read you file until its end.