我试图通过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 expressionsI'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.
更多推荐
发布评论