Java - How do I get last line using Scanner?

Posted by aloh on Stack Overflow See other posts from Stack Overflow or by aloh
Published on 2010-04-18T02:55:18Z Indexed on 2010/04/18 3:03 UTC
Read the original article Hit count: 313

Filed under:
        while( inStream.hasNextLine() )
        {
                ...

                lineList.add( inStream.nextLine() );
        }
        ...

lineList is an ArrayList. The code is reading everything nicely except it won't grab the last line. The last two lines in the text file end like this:

"a sentence here..."
<a blank line here. the blank line is the last line>

I'm assuming it won't grab it since hasNextLine() is not detecting another line after this one?

What's a way to grab the last line? I thought reading until it was EOF and then catching the exception might work but there doesn't seem to be a way to do that.

© Stack Overflow or respective owner

Related posts about scanner