• 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

EOF Exception while reading Data Section

 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic