Reading file using Java

Discussion in 'Java' started by Hyden Zeng, Jan 9, 2004.

  1. Hyden Zeng

    Hyden Zeng Guest

    Hi all,
    got one question regarding file reading.
    How do I directly access a certain byte or line of a file using Java?
    the time complexity should be O(1).
    Thank you.
    Hyden Zeng, Jan 9, 2004
    #1
    1. Advertising

  2. On 9 Jan 2004 00:18:10 -0800, Hyden Zeng wrote:
    > How do I directly access a certain byte or line of a file using
    > Java? the time complexity should be O(1).


    Open the file using a RandomAccessFile. Use seek() to get to the
    desired location. Use an appropriate read method from that class to
    get the data.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jan 9, 2004
    #2
    1. Advertising

  3. Hyden Zeng:

    >got one question regarding file reading.
    >How do I directly access a certain byte or line of a file using Java?
    >the time complexity should be O(1).


    You can access a certain byte by skipping over data with InputStream
    or calling the seek method with RandomAccessFile. The complexity
    should be O(1). However, extensions of InputStream that do not
    override the default implementation of skip(long) will read every byte
    before the position you want to go to, so no more O(1) there.

    As for accessing a certain line - that's not possible in O(1) without
    having some index file which stores the offsets of all lines in a text
    file.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
    Marco Schmidt, Jan 9, 2004
    #3
  4. Hyden Zeng wrote:

    > Hi all,
    > got one question regarding file reading.
    > How do I directly access a certain byte or line of a file using Java?
    > the time complexity should be O(1).


    To access a byte address, you can use java.io.RandomAccessFile, there's
    probably also an alternative in java.nio

    Accessing a certain line in O(1) is fundamentally impossible unless
    either line lengths are fixed (in which case a simple computation
    reduces it to the earlier problem) or you have a precomputed index
    that maps line numbers to byte offsets.
    Michael Borgwardt, Jan 9, 2004
    #4
  5. "Hyden Zeng" <> wrote in message
    news:...
    | Hi all,
    | got one question regarding file reading.
    | How do I directly access a certain byte

    RandomAcessFile

    |..or line of a file using Java?

    If it's FCW, use the formula ((LineNo-1)*LineWidth)
    to calculate which byte the line corresponds to,
    then RAF.

    Otherwise, the best you could do on a non-FCW
    file is a binary search, unless you are prepared to
    trawl throught he file once, and creat a second
    (much smaller) FCW 'index file' for the larger file.

    | ..the time complexity should be O(1).

    In a perfect world, yes. (I am just guessing
    what O(1) means)

    | Thank you.

    You're welcome..

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
    Andrew Thompson, Jan 9, 2004
    #5
  6. Hyden Zeng

    Roedy Green Guest

    On 9 Jan 2004 00:18:10 -0800, (Hyden Zeng) wrote
    or quoted :

    >got one question regarding file reading.
    >How do I directly access a certain byte or line of a file using Java?
    >the time complexity should be O(1).


    see http://mindprod.com/fileio.html
    to read by line.

    Then address the char you want with charAt()

    Unless you prebuild an index, there is no way to jump to a particular
    line. If you prebuild an index, you can then access with random io.
    http://mindprod.com/fileio.html
    will show you how.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jan 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    672
    Kevin Spencer
    Nov 11, 2004
  2. pps!
    Replies:
    0
    Views:
    1,027
  3. Replies:
    0
    Views:
    782
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    371
    Hrvoje Niksic
    May 25, 2007
  5. Replies:
    3
    Views:
    94
    David Squire
    Aug 5, 2006
Loading...

Share This Page