lgp
lgp

Reputation: 335

java- create simple byte offset index of a text file

I would like to index every 100th line of a very large text file with its corresponding 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?

Upvotes: 2

Views: 1328

Answers (3)

Perception
Perception

Reputation: 80603

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.

Upvotes: 1

unkx80
unkx80

Reputation: 96

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.

Upvotes: 0

Oscar Gomez
Oscar Gomez

Reputation: 18488

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.

Upvotes: 1

Related Questions