Java RandomAccessFile

编程入门 行业动态 更新时间:2024-10-26 00:30:08
Java RandomAccessFile - 处理不同的换行符样式?(Java RandomAccessFile - dealing with different newline styles?) java

我试图通过RandomAccessFile来寻找,并且作为算法的一部分,我必须读取一行,然后从行尾寻找

例如

String line = raf.readLine(); raf.seek (raf.getFilePointer() - line.length() + m.start() + m.group().length()); //m is a Matcher for regular expressions

我一直在收到大量的错误,并不知道为什么。 我刚刚发现这是因为我正在阅读的一些文件具有UNIX风格的换行符,\ r \ n,有些文件只是windows风格的\ n。

有没有一种容易让RandomAccessFile将所有换行符视为windows风格的换行符?

I'm trying to seek through a RandomAccessFile, and as part of an algorithm I have to read a line, and then seek backwards from the end of the line

E.g

String line = raf.readLine(); raf.seek (raf.getFilePointer() - line.length() + m.start() + m.group().length()); //m is a Matcher for regular expressions

I've been getting loads of off-by-one errors and couldn't figure out why. I just discovered it's because some files I'm reading from have UNIX-style linefeeds, \r\n, and some have just windows-style \n.

Is there an easy to have the RandomAccessFile treat all linefeeds as windows-style linefeeds?

最满意答案

您可以始终将流再备份两个字节并重新读取它们以查看它是\ r \ n还是(!\ r)\ n:

String line = raf.readLine(); raf.seek(raf.getFilePointer()-2); int offset = raf.read() == '\r' ? 2 : 1; raf.read(); //discard the second character since you know it is either \n or EOF by definition of readLine raf.seek (raf.getFilePointer() - (line.length()+offset) + m.start() + m.group().length());

我不确定你想要放置文件指针的确切位置,所以请适当调整2/1常量。 如果它们出现在文件中,您可能还需要为空白行添加额外的检查(\ n \ n),就好像它显示您可能会陷入无限循环而无法跨越它的代码一样。

You could always back the stream up two bytes and re-read them to see if it is \r \n or (!\r)\n:

String line = raf.readLine(); raf.seek(raf.getFilePointer()-2); int offset = raf.read() == '\r' ? 2 : 1; raf.read(); //discard the second character since you know it is either \n or EOF by definition of readLine raf.seek (raf.getFilePointer() - (line.length()+offset) + m.start() + m.group().length());

I'm not sure exactly where you are trying to place the file pointer, so adjust the 2/1 constants appropriately. You may also need to add an extra check for blank lines (\n\n) if they occur in your file, as if it shows up you might get stuck in an infinite loop without code to step past it.

更多推荐

本文发布于:2023-08-07 14:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   RandomAccessFile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!