wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes EOF Exception while reading Data Section Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "EOF Exception while reading Data Section" Watch "EOF Exception while reading Data Section" New topic
Author

EOF Exception while reading Data Section

satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi,

I am working on B&S 2.2.3. I just started reading my requirement. I have read the schema section and data section of the db.
As per requirement, I have read the values and printed on the console. I am getting EOF exception while reading the data section. I have given my code for review.
Can anyone give me pointer to resolve EOF Exception?
Is there any alternative way to find EOF in Random access file?

In the for loop, i have hardcoded the value 182 (record length) for time being. Is it correct to iterate the loop or Do i use raf.length() for iteration? I have to figure out the better way

satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
One small correction in the above code.
Its not 182 or record length. totalRecords indicate the number of records in the file. In otherwords, iterate through the eof.

Now I do not get EOF Exception. I would like to know the alternate solution to iterate through the end of file.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

What's wrong with your current solution?

That is, if you calculate the potential number of records in the file at runtime, and then iterate over that many records, you should not get an EOF exception.

As an aside, calculating the potential number of records at runtime can be a quick check that the data is in reasonable format (it is not corrupt).

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi Andrew,

I am calculating the total no of records using the following formula


Based on the the value of totalRecord, I am iterating the loop. I would like to know if there is any other better approach/solution available.

thanks for your response.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Hi



Given that you are using modulus here, you should always be getting zero records printed in the next line. Perhaps you meant to use division?

Otherwise this code looks good, and is the way I would recommend you find the number of records to iterate over.

Regards, Andrew
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
I am not using a RAF, but a DataInputStream to read the records from my data file. I catch the EOF exception and use this to stop reading, because the end of file is reached. I am not sure if this is correct usage of the EOF exception...


SCJP, SCJD
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi Andrew,

Thanks for pointing the improper usage of modulus operator. It was my mistake, I used modulus operator unintentionally . I have changed my code and I am using division operator now.

sat
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Originally posted by Jethro Borsje:
I am not using a RAF, but a DataInputStream to read the records from my data file. I catch the EOF exception and use this to stop reading, because the end of file is reached. I am not sure if this is correct usage of the EOF exception...


The thing about exceptions is that they are supposed to be exceptional (unexpected) situations. In addition, when they do occur, the creation of the Exception object is a heavyweight process (generally not desirable).

In this particular case, you know how long records are, you know how long the file is, and you know how large the meta-data is. Therefore you should know how many records you have (as Satishkumar demonstrates). Given that, you should be able to construct code that only ever reads data that is really there, and an EOFException will truly be an unexpected situation, deserving to be processed outside of normal application flow.

As an aside, you mention that you are using DataInputStream to read the data - what about writing records back out? If you only need to update record number 5, how do you go about doing that?

Regards, Andrew
Jethro Borsje
Ranch Hand

Joined: Jul 22, 2008
Posts: 100
Originally posted by Andrew Monkhouse:
The thing about exceptions is that they are supposed to be exceptional (unexpected) situations. In addition, when they do occur, the creation of the Exception object is a heavyweight process (generally not desirable).

In this particular case, you know how long records are, you know how long the file is, and you know how large the meta-data is. Therefore you should know how many records you have (as Satishkumar demonstrates). Given that, you should be able to construct code that only ever reads data that is really there, and an EOFException will truly be an unexpected situation, deserving to be processed outside of normal application flow.

I think you are right about that, using the EOFException felt wrong all along, but it's so very convenient that I kept it in untill now. I will definitely adapt my code in order to not use the EOFException.

Originally posted by Andrew Monkhouse:
As an aside, you mention that you are using DataInputStream to read the data - what about writing records back out? If you only need to update record number 5, how do you go about doing that?

At this point I have not yet implemented the "write records back" code. My approach is to read the entire data file into memory (using a Map) when the server starts.

At first I wanted to write the entire record map back to the data file when the server shuts down. However, this has two drawbacks:
[1] - if the server crashes the updated data is lost
[2] - I write more data then is actually needed if not all records are updated

So thinking about this I have decided to use a RAF when reading all data into the Map. If a record is updated (or added / deleted) I use this RAF to change the data file right away.

I think this is a better approach.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EOF Exception while reading Data Section
 
Similar Threads
URLyBird db format
B&S:db format question
IOException thrown
How to check the B&S db file is in correct format?
B&S: problem in the db file processing