• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to go to last line of a file and skipping the rest?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this is my temporary code.
how can i directly read only the last line without reading the rest?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. You would simply have to ignore the preceding lines.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there is a kludgy alternative if you can be sure that the last line is less than X characters long.

You could open the file as a RandomAccessFile, seek to the end minus X bytes, read the last X bytes into a byte array and look backwards through that for the start of the last line. Turning the bytes located into a String is simple.

Bill
 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

But how can i ignore rest of the lines??


Randomacces file would have been good. but the size of the file is not a constant!!
 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cant i place a delimiter and do this?

is there any possibitlity with tokens and stuff?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

january radolf wrote:But how can i ignore rest of the lines??



In programming, you "ignore" something by just not doing anything. So simply read through the file one line at a time. For each line, store it in a variable but don't do anything with it.

After you reach the end of the file, the last line you read was (naturally) the last line in the file. You just assigned it to a variable, though. Now do something with it.
 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks paul for your reply
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But how can i ignore rest of the lines??

Randomacces file would have been good. but the size of the file is not a constant!!



1. By using RandomAccessFile seek() method you automatically ignore all preceeding bytes.

2. RandomAccessFile class provides the length() method so it doesn't matter that the file size varies. Where in the world did you get the idea that it did matter??


Reading line by line and ignoring all but the last line will work but is horribly inefficient for all but the shortest files.

Bill
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic