Affinity IT
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » I/O and Streams
 
RSS feed
 
New topic
Author

Problem obtaining last record of a Binary File

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Messages: 151

Hello!
Thank you very much for the replies to my inquiries.

This is in connection to my previous posts here and here.

I am now successful on obtaining the correct data from a Binary file using RandomAccessFile. However, I could not seem to understand why I couldn't get the last record (or imaginary row) of my binary file.

My program just reads data from an Excel then writes/appends each field to a Binary File. On writing to the Binary file, I use DataOutputStream. Then on reading, I use RandomAccessFile as abovementioned. My excel has 100 records. Theoretically, I should expect 100 records as well, written in the binary file. But my program prints 99 records only. I checked the sizes after writing using DataOutputStream's size() and before reading using RandomAccessFile's length() and they're the same.

Here's my loop on reading the binary file:



I hope you could point to me if there's something wrong with my approach, I would really appreciate it.
Thanks in advance!>
rk palleru
Greenhorn

Joined: Feb 26, 2009
Messages: 22

1.Here retrieving values "i<size" , It retrieves length-1 then we could not get last file. so use "i<=size" in for loop.

2. In if(i%99==0) means if i value is 99 ,then (99%99==0) next time (100%99==0) in this case if condition does not satisfy









Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Messages: 151

rk palleru wrote:1.Here retrieving values "i<size" , It retrieves length-1 then we could not get last file. so use "i<=size" in for loop.

2. In if(i%99==0) means if i value is 99 ,then (99%99==0) next time (100%99==0) in this case if condition does not satisfy



You are actually correct. I overlooked my logic.
Thanks very much for the suggestion.

This message was edited 1 time. Last update was at by Denise Advincula

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » I/O and Streams
 
RSS feed
 
New topic
The most intelligent Java IDE