• 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

Looping over the contents of the file...

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy folks,
What would you say is the standard way to loop over the contents of the file?
I do the following but am convinced there is a better way:



Thanks,
Best regards.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why are you looping?
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To read every row...?
 
Mike Ngo
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The RandomAccessFile is used to randomly access any byte in a file so typically you would do something like this

RandomAccessFile stream = // open it
// calculate file pointer
long offset = calculateOffset(my_record);
stream.seek(offset)
// read my record

If you don't need to randomly access the data, you can use DataInput/OutputStream.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean, but i don't see how you would use RandomAccessFile to read ALL rows? And if the answer is to use a DataInputStream, then how can i read a certain amount of bytes for each field like RandomAccessFile does? How do i read each row with a RandomAccessFile? Or is the trick to mix it with a DataInputStream...?

Kind regards,
Mars.
 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcelo Ortega:
I see what you mean, but i don't see how you would use RandomAccessFile to read ALL rows? And if the answer is to use a DataInputStream, then how can i read a certain amount of bytes for each field like RandomAccessFile does? How do i read each row with a RandomAccessFile? Or is the trick to mix it with a DataInputStream...?

Kind regards,
Mars.



I think the easyest way is to keep track of how many records (deleted and active) are in the file.

Then you will do something like the following:



To calculate the number of records would be something like:

numrecords= (fileSize - HeaderLength) / recordLength + 1 //Dont forget about active delete flag
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic