Results 1 to 2 of 2
  1. #1
    lunarbof is offline Member
    Join Date
    Aug 2008
    Rep Power

    Default Random access to text file


    I have a text file where each line correspond to a specific entry. Unfortunately, lines are of variable size.

    At some point, I need to access entry X, which I know to be at line X in my text file. Unfortunately, I have no clue what is the byte position of entry X. Of course, I want to avoid reading all of the X-1 first lines.

    I am wondering if there is any way to access a text file randomly based on line number instead of byte position ?

    If not, suggestions are welcome.
    An obvious solution would be to keep an index file with entries of the form:
    "Entry X : byte position of entry X in data file"
    So each line of the index file has a constant size.However, this is a bit tedious as modification to the main file implies modification of the whole index file.... other solutions ?

    Thanks !

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    solution would be to keep an index file
    That doesn't require each line be that same size. If each line were the same size, then you don't need an index file. You can compute the position of the line directly.
    The index file would require a one time scan of the file to find and save the location of each line. If the length of a line were changed, the index for the following lines would have to be adjusted accordingly.

Similar Threads

  1. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  2. Cannot display a random string from .dat file
    By explosion242 in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 01:48 PM
  3. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 PM
  4. Random Access Files concept
    By AralX in forum New To Java
    Replies: 2
    Last Post: 12-25-2007, 07:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts