开发者

java- create simple byte offset index of a text file

开发者 https://www.devze.com 2023-03-22 01:27 出处:网络
I would like to index every 100th line of a very large text file with its correspon开发者_开发知识库ding byte offset. As I\'m reading through the file to create my index with a bufferedreader, is it p

I would like to index every 100th line of a very large text file with its correspon开发者_开发知识库ding byte offset. As I'm reading through the file to create my index with a bufferedreader, is it possible to figure out which byte position I am at?


You can use:

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException

And use the return value int, which is the numbers of characters read, then keep this information on a counter, so that when you have read 100 of this:

System.getProperty("line.separator");

you can use this counter to get the byte position you are at.


You could use a RandomAccessFile. Use the readLine method to get the next N lines of text, then determine your current position in the file using the getFilePointer method.

The one caveat is that this cannot handle reading in Unicode strings.


Using BufferedReader is no good, unless you can be sure that your lines are all ASCII and the linebreaks are consistent (either all CR+LF or all LF only). I suggest you use BufferedInputStream and and search for '\n' instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号