Java RandomAccessFile - dealing with different newline styles?

Posted by waitinforatrain on Stack Overflow See other posts from Stack Overflow or by waitinforatrain
Published on 2010-03-24T06:00:58Z Indexed on 2010/03/24 6:03 UTC
Read the original article Hit count: 323

Filed under:
|
|

Hey,

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about randomaccessfile