Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Is there a way to read a specific line in a txt file (without iterating through)?

  1. #1
    TheNadz is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Is there a way to read a specific line in a txt file (without iterating through)?

    Obviously I could just iterate through lines until I get to the line I need but that seems very inefficient, especially since I'm working with ~58,000 lines. Is there a way in the Java API to go directly to a set line?

    EDIT: For now I've just read all values into an ArrayList and then pulled values from there but if anyone has a better solution then feel free to post it.
    Last edited by TheNadz; 06-26-2011 at 07:12 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power


    Unless the lines are all the same length, there's no way to know the offset of any particular line without reading all the lines before it.

    If the file doesn't change much, you could read it once and create an index file that stores the offsets of the beginnings of each line. Then you could go directly to any line using the class that sunde linked.
    Fubarable likes this.
    Get in the habit of using standard Java naming conventions!

  4. #4
    Ashish_jain's Avatar
    Ashish_jain is offline Member
    Join Date
    Jun 2011
    Rep Power

Similar Threads

  1. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  2. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  3. Write to a specific line in a file
    By phantom06 in forum New To Java
    Replies: 1
    Last Post: 02-05-2011, 06:32 PM
  4. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 10:40 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 AM

Posting Permissions

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