Martin Pernollet
Martin Pernollet

Reputation: 2315

Indexing text files in java

I have a set of text files providing informations that are parsed, analysed and allow building a model. Sometime, the user of this model wants to know which part of a text file was used to generate a given model item.

For that I am thinking of keeping track of the range of lines (or bytes) ids to be able to read the appropriate text part once required.

My question is: I wonder if it their exists any java Reader able to read a file by using a start and stop line (or byte) id instead of reading the file from the begining and counting the lines (bytes)?

Best regards

Upvotes: 0

Views: 2157

Answers (3)

leventov
leventov

Reputation: 15263

To read from the certain byte - SeekableByteChannel. Of cause, there aren't any Readers able to start from the line id - because positions of line separators are unknown.

Upvotes: 2

AlexR
AlexR

Reputation: 115328

You can use InputStream.mark() and InputStream.skip() to navigate to concrete position into the file.

But are you sure you really have to implement this yourself? Take a look on Lucine - the indexing service that probably will help you.

Upvotes: 1

Anton
Anton

Reputation: 6051

If you know exactly amount of bytes, that should be skipped, you can use seek method method of RandomAccessFile

Upvotes: 6

Related Questions