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.
精彩评论