Java Question: Have a very large txt file and want to start reading in the middle

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
I have very large txt files (several hundred megabytes) that I would like to start reading the lines in the middle. For example, say the file looks like:

Scan 1
random text
Scan 2
more random text
Scan 3
...
Scan 100,000

Say I want to read the random text under Scan 50,000. How would I do this without reading the lines from Scan 1 to 49,999?

And before someone asks, no it is not possible to reformat the files to make the smaller.

Thanks for any help.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Note that just using random access won't help you if you don't know where to go. In their example they are talking about ZIP files, which basically have an internal table of contents that describes where each individual file in the archive starts.

If you don't have some kind of index, you'd have to do a search to find where the entry you are interested in starts. A binary search would work well if the entries are all in order.
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
Random Access Files seems to be working great. Thanks MrChad.

Matthias: at the moment I just readLine() and use Scanner to parse the text to find the "Scan #" that I'm looking for. Still doing some testing, but so far it's working.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: flashbacck
Random Access Files seems to be working great. Thanks MrChad.

Matthias: at the moment I just readLine() and use Scanner to parse the text to find the "Scan #" that I'm looking for. Still doing some testing, but so far it's working.

...that will work, but in that case you're just reading the whole file like before (although it's probably a faster implementation). Didn't you want to not do that?
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
Originally posted by: Matthias99
Originally posted by: flashbacck
Random Access Files seems to be working great. Thanks MrChad.

Matthias: at the moment I just readLine() and use Scanner to parse the text to find the "Scan #" that I'm looking for. Still doing some testing, but so far it's working.

...that will work, but in that case you're just reading the whole file like before (although it's probably a faster implementation). Didn't you want to not do that?

How do you mean?

I'm doing a binary search by: seeking to a byte in the middle of the file, readLine() and use Scanner to parse the line, if I'm at the wrong spot I seek to another spot.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: flashbacck
Originally posted by: Matthias99
Originally posted by: flashbacck
Random Access Files seems to be working great. Thanks MrChad.

Matthias: at the moment I just readLine() and use Scanner to parse the text to find the "Scan #" that I'm looking for. Still doing some testing, but so far it's working.

...that will work, but in that case you're just reading the whole file like before (although it's probably a faster implementation). Didn't you want to not do that?

How do you mean?

I'm doing a binary search by: seeking to a byte in the middle of the file, readLine() and use Scanner to parse the line, if I'm at the wrong spot I seek to another spot.

You left out the crucial details like seeking to the middle of the file. :p

From your description I thought you were just reading each line sequentially with readLine().