aspose file tools*
The moose likes Beginning Java and the fly likes Reading the last (or last but 1) line in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading the last (or last but 1) line in a text file" Watch "Reading the last (or last but 1) line in a text file" New topic
Author

Reading the last (or last but 1) line in a text file

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Is there an easy way to skip lines in a text file?

I have a text file which reads a bit like this, where %n means newline, and most Britons will recognise AB123456C as a National Insurance number:-

0 1 2 3 4 Campbell Ritchie AB123456C%n
0 12 23 34 45 Campbell Ritchie AB123456C%n
0 123 234 345 456 Campbell Ritchie AB123456C%n
0 1234 2345 3456 4567 Campbell Ritchie AB123456C%n

. . . and I can easily get a java.util.Scanner to read the lines.

But, what I really want is to get it to skip the 1st 2nd and 3rd lines and read the 4th line beginning 0 1234. I have tried all sorts of combinations of while(myScanner.hasNext()){}, while(myScanner.hasNextLine()), myScanner.nextInt(), myScanner.next(), and it happily reads either one line or all five lines (the %n at the end of the 4th line means there is a 5th line with nothing in).

I might use two Scanners, use one to go

. . . but I think there must be a more elegant way to do it.
I don't want to go back to the old J1.4 way of using FileStreams or whatever they were called, because then I have to call Integer.parseInt() for each of the numbers, and scanner2.nextInt() is so much neater.

Any suggestions???
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I didn't recognize the National Insurance Number, but then again cricket doesn't make sense to me either.
Still, this may be what you're looking for:


There is no emoticon for what I am feeling!
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
A National Insurance Number is the U.K. equivalent of a Social Security Number in the U.S.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Yes, that probably is what I am looking for, Mr Albreachtsen.
It is obvious once you think of it.

Thank you.
Allion Salvador
Ranch Hand

Joined: Nov 15, 2005
Posts: 49
YOU SPELLED HIS NAME WRONG!!!


Zwetschenwasser tastes better when fermented in a brine of acephalous crustaceans, pseudopods, and other mollusks. It goes quite well with gnocchi and mole.
Allion Salvador
Ranch Hand

Joined: Nov 15, 2005
Posts: 49
Oh, and they are called "FileInputStream" and I think they are fine!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Allion Salvador:
YOU SPELLED HIS NAME WRONG!!!


That's OK. It's only my nom de guerre.
Ricardo Wagemaker
Greenhorn

Joined: Oct 10, 2012
Posts: 16

Jeff Albertson wrote:I didn't recognize the National Insurance Number, but then again cricket doesn't make sense to me either.
Still, this may be what you're looking for:


Hi Jeff,

Old thread but this is exacly what I wanted, except one small detail and I can't figure it out and that is how do I get the 3rd from last line also.

I have before = penultimate and then penultimate = scanner.nextLine(); also but it does not work :-(

Please help if you are still about.

Many Thanks
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

you need a third variable penulitmatePenultimate
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Campbell Ritchie,
Your post was moved to a new topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Campbell Ritchie wrote:Campbell Ritchie,
Your post was moved to a new topic.
Talking to yourself again? Sure sign of insanity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading the last (or last but 1) line in a text file