Reading LAST line from text file without iterating through the file?

Discussion in 'Java' started by Robin Wenger, Feb 23, 2011.

  1. Robin Wenger

    Robin Wenger Guest

    Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?

    Robin
    Robin Wenger, Feb 23, 2011
    #1
    1. Advertising

  2. Re: Reading LAST line from text file without iterating through thefile?

    On 02/23/2011 07:59 AM, Robin Wenger wrote:
    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?
    >
    > Robin
    >


    You could use a RandomAccessFile and search backwards from the end for a
    linefeed. Depending on the size of the line and the size of the file,
    it might not be more efficient than reading the whole file.

    --

    Knute Johnson
    s/nospam/knute2011/
    Knute Johnson, Feb 23, 2011
    #2
    1. Advertising

  3. Re: Reading LAST line from text file without iterating through thefile?

    On 02/23/2011 07:59 AM, Robin Wenger wrote:
    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?
    >
    > Robin
    >


    You could use a RandomAccessFile and search backwards from the end for a
    linefeed. Depending on the size of the line and the size of the file,
    it might not be more efficient than reading the whole file.

    --

    Knute Johnson
    s/nospam/knute2011/
    Knute Johnson, Feb 23, 2011
    #3
  4. Robin Wenger

    Lew Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On 02/23/2011 10:59 AM, Robin Wenger wrote:
    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?


    Yes, but it's tricky. You need a random-access file and seek backwards to a
    newline.

    --
    Lew
    Honi soit qui mal y pense.
    Lew, Feb 23, 2011
    #4
  5. Re: Reading LAST line from text file without iterating through thefile?

    On 23/02/2011 18:21, Lew allegedly wrote:
    > On 02/23/2011 10:59 AM, Robin Wenger wrote:
    >> Is it possible to read the last text line from a text file WITHOUT
    >> reading the previous (n-1) lines?

    >
    > Yes, but it's tricky. You need a random-access file and seek backwards
    > to a newline.
    >


    $ echo RandomAccessFile | hivemind | cut
    Daniele Futtorovic, Feb 23, 2011
    #5
  6. Robin Wenger

    Tom Anderson Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On Wed, 23 Feb 2011, Robin Wenger wrote:

    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?


    Yes.

    tom

    --
    What? Yeah!
    Tom Anderson, Feb 23, 2011
    #6
  7. Robin Wenger

    Luuk Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On 23-02-11 19:49, Daniele Futtorovic wrote:
    > On 23/02/2011 18:21, Lew allegedly wrote:
    >> On 02/23/2011 10:59 AM, Robin Wenger wrote:
    >>> Is it possible to read the last text line from a text file WITHOUT
    >>> reading the previous (n-1) lines?

    >>
    >> Yes, but it's tricky. You need a random-access file and seek backwards
    >> to a newline.
    >>

    >
    > $ echo RandomAccessFile | hivemind | cut


    bash: hivemind: command not found

    --
    Luuk
    Luuk, Feb 23, 2011
    #7
  8. Robin Wenger

    Eric Guest

    Eric, Feb 23, 2011
    #8
  9. Re: Reading LAST line from text file without iterating through thefile?

    On 23/02/2011 20:27, Luuk allegedly wrote:
    > On 23-02-11 19:49, Daniele Futtorovic wrote:
    >> On 23/02/2011 18:21, Lew allegedly wrote:
    >>> On 02/23/2011 10:59 AM, Robin Wenger wrote:
    >>>> Is it possible to read the last text line from a text file WITHOUT
    >>>> reading the previous (n-1) lines?
    >>>
    >>> Yes, but it's tricky. You need a random-access file and seek backwards
    >>> to a newline.
    >>>

    >>
    >> $ echo RandomAccessFile | hivemind | cut

    >
    > bash: hivemind: command not found


    cljp: hivemind: ooh yeah!

    ;)
    Daniele Futtorovic, Feb 23, 2011
    #9
  10. Robin Wenger

    Ian Shef Guest

    (Robin Wenger) wrote in
    news:4d652ee7$0$6877$-online.net:

    > Is it possible to read the last text line from a text file WITHOUT
    > reading the previous (n-1) lines?
    >
    > Robin
    >
    >

    Yes, under certain circumstances. For example, if you know "n" and know that
    all of the lines are of some fixed length (also known). There are other
    situations as well.
    Ian Shef, Feb 24, 2011
    #10
  11. Robin Wenger

    Eric Sosman Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On 2/23/2011 10:59 AM, Robin Wenger wrote:
    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?


    Others have mentioned using RandomAccess to work backward from the
    end of the file until you find the penultimate line-ending. This can
    work, but it can also fail. Consider a file with context-sensitive
    encoding, for example, where the meaning of a byte depends on the values
    of bytes that precede it. If you read an isolated byte of value 91 from
    such a file, without knowing whether it's a free-standing character or a
    part of a multi-byte sequence or possibly preceded by a "shift-out," you
    won't know what that byte value means.

    One strategy is to estimate a typical line length of N characters,
    seek to 100*N (say) bytes before the end, and start reading from
    there. A nice feature of most multi-byte encoding schemes is that they
    tend to self-synchronize: You may get misinterpreted garbage for a
    while, but things are likely to get back on track eventually. If you
    want to get fancy you can apply reasonability tests to what you (think
    you've) read, and restart at END-1000*N if things seem unreasonable.

    --
    Eric Sosman
    d
    Eric Sosman, Feb 24, 2011
    #11
  12. Robin Wenger

    Arne Vajhøj Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On 23-02-2011 10:59, Robin Wenger wrote:
    > Is it possible to read the last text line from a text file WITHOUT reading the previous (n-1) lines?


    In general no.

    All the RandomAccessFile tricks are based on assumptions about lines
    being separated by something - they do not work with record formats
    that contains a line length instead of a delimiter.

    If Unix/Linux/Windows/MacOS X is all you need to support then try:

    public static String readLastLineUnSup(String fnm) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(fnm, "r");
    String res = "";
    long ix = raf.length() - 1;
    for(;;) {
    raf.seek(ix);
    int c = raf.read();
    if(c == '\r' || c == '\n') break;
    res = (char)c + res;
    ix--;
    }
    raf.close();
    return res;
    }

    Arne
    Arne Vajhøj, Feb 24, 2011
    #12
  13. Robin Wenger

    Eric Sosman Guest

    Re: Reading LAST line from text file without iterating through thefile?

    On 2/23/2011 10:23 PM, Ken Wesson wrote:
    > On Wed, 23 Feb 2011 21:21:42 -0500, Arne Vajhøj wrote:
    >
    >> On 23-02-2011 10:59, Robin Wenger wrote:
    >>> Is it possible to read the last text line from a text file WITHOUT
    >>> reading the previous (n-1) lines?

    >>
    >> In general no.
    >>
    >> All the RandomAccessFile tricks are based on assumptions about lines
    >> being separated by something - they do not work with record formats that
    >> contains a line length instead of a delimiter.

    >
    > "Record formats" are not relevant here, nor was someone else's concern
    > about compressed formats -- the OP clearly said "a text file", by which
    > is generally understood flat ASCII with CR, LF, or CRLF as line delimiter.


    OpenVMS supports many record formats, but the "native" one for
    text files is VAR: A two-byte binary count, the payload characters,
    and if necessary a padding byte to make the total byte count even.

    The "next most native" format is VFC, which is sort of like VAR
    except that the first N (fixed) bytes of the payload are metadata
    (line numbers, carriage control, ...) instead of line content.

    Then come the easy formats: STREAM, STREAM-LF, STREAM-CR, and
    FIXED. Oh, yes, and UNDEF; let's not forget UNDEF (although, to be
    honest, UNDEF is more commonly used for "binary" than "text" files).

    (Strangest text file format I ever ran into used line-*bracketing*
    characters: a CR before and an LF after. The rationale for this format
    caused me to shake my head and sigh: It was said that as you printed
    such a file on a typewriter-like console, possibly with long pauses
    between lines for progress messages and the like, then the LF at end-
    of-line would move the paper so the print head wouldn't interfere with
    reading it. As I said, shake the head.)

    In short, all I'm asking is that you delete the word "generally"
    because your experience is insufficiently general.

    --
    Eric Sosman
    d
    Eric Sosman, Feb 24, 2011
    #13
  14. Robin Wenger

    Lars Enderin Guest

    Re: Reading LAST line from text file without iterating through thefile?

    2011-02-24 15:00, Ken Wesson skrev:
    > On Thu, 24 Feb 2011 21:23:34 +0800, Peter Duniho wrote:
    >
    >> On 2/24/11 9:06 PM, Ken Wesson wrote:
    >>> [...]
    >>> Obsolete systems do not interest me.

    >>
    >> then…
    >>
    >>> Since those days, the world has standardized on ASCII flat files for
    >>> text files.

    >>
    >> LOL!

    >
    > Windows text files are flat ASCII files (with CRLF line ends). Mac text
    > files are flat ASCII files (with CR line ends). Unix text files are flat
    > ASCII files (with LF line ends). And that exhausts 99.99% of the
    > operating system market share right there, if not more, not counting
    > smartphones which are all too modern to be using weird legacy formats for
    > text files.
    >
    > I can't remember the last time I had to interoperate with any machine
    > that had anything other than standard ASCII as the native format for text
    > files. It's gotta be decades.


    ASCII character values are limited to the 0-127 range. That's an
    outdated "standard".
    Lars Enderin, Feb 24, 2011
    #14
  15. Re: Reading LAST line from text file without iterating through thefile?

    Ken Wesson wrote:
    > On Thu, 24 Feb 2011 00:42:41 -0500, Eric Sosman wrote:
    >> On 2/23/2011 10:23 PM, Ken Wesson wrote:
    >>> "Record formats" are not relevant here, nor was someone else's concern
    >>> about compressed formats -- the OP clearly said "a text file", by which
    >>> is generally understood flat ASCII with CR, LF, or CRLF as line
    >>> delimiter.


    Ah, the warm blanket of provincialism.

    >> OpenVMS supports many record formats, but the "native" one for
    >> text files is VAR: A two-byte binary count, the payload characters, and
    >> if necessary a padding byte to make the total byte count even.
    >> ...
    >> In short, all I'm asking is that you delete the word "generally"
    >> because your experience is insufficiently general.


    On the IBM i machines (formerly i Series, formerly System i, formerly
    AS/400, successor to the System/3x), using the default filesystem, a
    text "file" is actually a series of records in a "member" of a
    "physical file". The i operating system hides implementation details,
    but access to the contents of the "file" is record-oriented, not
    byte-oriented.

    In the alternate Hierarchical File System supported by the i machines
    for POSIX compatibility, text files are byte-oriented, but usually
    EBCDIC, not ASCII.

    On IBM and other EBCDIC mainframe systems, there are a variety of
    formats for text files, but flat byte-oriented ASCII isn't one of
    them, unless you're running Linux.

    > Obsolete systems do not interest me.


    Apparently, neither do prominent ones that you don't happen to know
    about. What a surprise.

    > Since those days, the world has
    > standardized on ASCII flat files for text files.


    Only for sufficiently small values of "the world".

    --
    Michael Wojcik
    Micro Focus
    Rhetoric & Writing, Michigan State University
    Michael Wojcik, Feb 24, 2011
    #15
  16. Ken Wesson writes:

    > On Thu, 24 Feb 2011 21:23:34 +0800, Peter Duniho wrote:
    >
    > > On 2/24/11 9:06 PM, Ken Wesson wrote:
    > >> [...]
    > >> Obsolete systems do not interest me.

    > >
    > > then…
    > >
    > >> Since those days, the world has standardized on ASCII flat files
    > >> for text files.

    > >
    > > LOL!

    >
    > Windows text files are flat ASCII files (with CRLF line ends). Mac
    > text files are flat ASCII files (with CR line ends). Unix text files
    > are flat ASCII files (with LF line ends). And that exhausts 99.99%
    > of the operating system market share right there, if not more, not
    > counting smartphones which are all too modern to be using weird
    > legacy formats for text files.
    >
    > I can't remember the last time I had to interoperate with any
    > machine that had anything other than standard ASCII as the native
    > format for text files. It's gotta be decades.


    I remember when we used a seven-bit character code to write my native
    language. We could toggle the way we viewed the character codes where
    we had put those characters that were not in ASCII. It was either
    brackets and braces or those letters, but never both.

    V{nkyr{-{{kk|si{. It's not a happy memory.
    Jussi Piitulainen, Feb 24, 2011
    #16
  17. Robin Wenger

    Lars Enderin Guest

    Re: Reading LAST line from text file without iterating through thefile?

    2011-02-24 15:19, Jussi Piitulainen skrev:
    > Ken Wesson writes:
    >
    >> On Thu, 24 Feb 2011 21:23:34 +0800, Peter Duniho wrote:
    >>
    >>> On 2/24/11 9:06 PM, Ken Wesson wrote:
    >>>> [...]
    >>>> Obsolete systems do not interest me.
    >>>
    >>> then…
    >>>
    >>>> Since those days, the world has standardized on ASCII flat files
    >>>> for text files.
    >>>
    >>> LOL!

    >>
    >> Windows text files are flat ASCII files (with CRLF line ends). Mac
    >> text files are flat ASCII files (with CR line ends). Unix text files
    >> are flat ASCII files (with LF line ends). And that exhausts 99.99%
    >> of the operating system market share right there, if not more, not
    >> counting smartphones which are all too modern to be using weird
    >> legacy formats for text files.
    >>
    >> I can't remember the last time I had to interoperate with any
    >> machine that had anything other than standard ASCII as the native
    >> format for text files. It's gotta be decades.

    >
    > I remember when we used a seven-bit character code to write my native
    > language. We could toggle the way we viewed the character codes where
    > we had put those characters that were not in ASCII. It was either
    > brackets and braces or those letters, but never both.
    >
    > V{nkyr{-{{kk|si{. It's not a happy memory.


    I have the same experience. C code wasn't very readable with "Swedish
    ASCII". At least Finnish doesn't use "Ã¥", except when quoting Swedish words.
    Lars Enderin, Feb 24, 2011
    #17
  18. Robin Wenger

    Lars Enderin Guest

    Re: Reading LAST line from text file without iterating through thefile?

    2011-02-24 15:26, Peter Duniho skrev:
    > On 2/24/11 10:14 PM, Lars Enderin wrote:
    >> ASCII character values are limited to the 0-127 range. That's an
    >> outdated "standard".

    >
    > Used by "obsolete systems". A key point in my amusement. :)


    I thought so, but Ken seemed to need an explanation.
    Lars Enderin, Feb 24, 2011
    #18
  19. Re: Reading LAST line from text file without iterating through thefile?

    On 24/02/2011 14:00, Ken Wesson wrote:
    > Windows text files are flat ASCII files (with CRLF line ends).


    Actually I find that, nowadays, lots of text files on Windows are
    so-called 'ANSI' (mostly CP-1252) or 'Unicode' (usually meaning UTF-16
    with BOM).

    Even on my ancient XP boxes, Notepad offers only ANSI, Unicode, Unicode
    big-endian and UTF-8. Wordpad offers RTF, Text-Document (turns out to be
    CP-1252), Text-Document DOS format (turns out to be CP-850) and
    Unicode. No ASCII.


    --
    RGB
    RedGrittyBrick, Feb 24, 2011
    #19
  20. Re: Reading LAST line from text file without iterating through thefile?

    On 24/02/2011 19:46, Ken Wesson allegedly wrote:
    > it's not (...) ASCII (...).


    Spot on.
    Daniele Futtorovic, Feb 24, 2011
    #20
    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. Deivys Ramirez
    Replies:
    3
    Views:
    344
    Asun Friere
    Aug 29, 2007
  2. BlueBird
    Replies:
    4
    Views:
    294
    Gabriel Genellina
    Sep 17, 2007
  3. carl
    Replies:
    5
    Views:
    2,318
    James Kanze
    Nov 25, 2009
  4. curious
    Replies:
    1
    Views:
    161
    Patrick Spence
    Oct 25, 2006
  5. Replies:
    20
    Views:
    299
    Michele Dondi
    Jan 16, 2005
Loading...

Share This Page