File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping over the contents of the file...

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Ngo
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you looping?
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To read every row...?
 
Mike Ngo
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Smyth
Ranch Hand
Posts: 288
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic