aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S How to read numeric values from database file 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 "B&S How to read numeric values from database file" Watch "B&S How to read numeric values from database file" New topic
Author

B&S How to read numeric values from database file

Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
Hi
In the beginning of my database file i have the following:
4 byte numeric, magic cookie value. Identifies this as a data file
4 byte numeric, total overall length in bytes of each record
2 byte numeric, number of fields in each record

In order to read this from the file I'm using a RandomAccessFile in the following way:

int magicValue = raf.readInt();
int recordLength = raf.readInt();
short fieldsInRecord = raf.readShort();

Should I instead read the specified number of bytes? Or is it ok to use these "shortcuts" ?

Thanks


SCJP 5<br />SCJD
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
You and I have the same problem. Did you already solved that problem?
Soroj Margun
Ranch Hand

Joined: Jun 15, 2008
Posts: 44
Hi all,

It is depends on your design. You can use anything but do not forget to state the reason of using in the document. But in my opinion, reading specified no. of byte via readXXX() is not the good way. Because if the structure of database is changed, you program will be immediately unusable.

Hope this help.


SCJP 1.2; SCWCD 1.2,1.4; SCBCD 1.3; SCJD 5.0
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
reading specified no. of byte via readXXX() is not the good way. Because if the structure of database is changed, you program will be immediately unusable.


How you read that bytes? I assume that the structure of file from the start fo file until the schema section will never change. It is not written in my instruction that the strucure of database will change.

thanks

Jeffry Kristianto Yanuar
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2)
Soroj Margun
Ranch Hand

Joined: Jun 15, 2008
Posts: 44
Hi Jeffry,

How fast !!! You're right, the spec. is not say about structure changing at all. But spec. said, your program should be easily to maintain by a junior programmer. Using readXXX() may not be easily to find by a junior when your program behave abnormal. Mine solution for reading structured database is using config file. With the configable file, my junior will easily check and change it to make it compatible with the current database structure if changing is occured.

Cheers
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Your solution is not bad either, but when I was a junior programmer, I knew what the readXXX() method does. If I couldn't know that method, I could easily find that method on Java API library.

the term "junior programmer" has more than one definitions. Right?
Soroj Margun
Ranch Hand

Joined: Jun 15, 2008
Posts: 44
Hi Jeffry,

Absolutely yes. But my point is, changing the code everytime database structure changed (especially a minor one like changing length of the field) may not be a good choice. With config file, maintainer can easily understand and possible to change program's behavior at run-time. Changing code is a little bit more complex (and sometime impossible or illegal if using binary lib).

Just share idea.
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
How long you finish your assignment?
Soroj Margun
Ranch Hand

Joined: Jun 15, 2008
Posts: 44
About 2 months of a few hours after day-work and no-busy weekends.
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Do yo have a particular reason for taking SCJD ?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11527
    
100

Originally posted by Soroj Margun:
But my point is, changing the code everytime database structure changed (especially a minor one like changing length of the field) may not be a good choice.

Ahh, but it is a choice, and as long as it can be justified then it is fine.

Personally I think that changing the length of a field is not such a minor change. If it were just a minor field (e.g. cookie value length) then you might be able to get away with just writing a quick and dirty program to read the old file and write the new file without caring about contents. But most other fields (e.g. bytes in a record, number of fields in a record) would require some sort of conversion program - either a program that simultaneously understands both old and new formats, or 2 programs (one that writes records to an interim file (CSV?) and one that reads that file and creates the new database). Either way there is a decent amount of work involved. So changing something like a readShort to readInt is (in my opinion) the least of the problems.

As for my personal opinion on the original question - I would certainly prefer to see code that used the "shortcuts" than having to read code that essentially duplicates the J2SE standard library. (Isn't there a clause in the instructions about not writing your own code when there is a standard method that does the same function?)

Regards, Andrew


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

Joined: Oct 01, 2007
Posts: 759
Andrew, the instructions said that there is 2 byte flag with each record. 00 for valid record and 0x8000 for deleted record. The issue is, 0x8000 is integer (4 byte).

Do I have to cast that int to short (2 byte) and document my assumptions? My assumptions is 0x8000 must be cast to short which value will become
-32768. This value is just for mark, not for calculation.

Thanks

Jeffry Kristianto Yanuar
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2)
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11527
    
100

Jeffry

This is a different question than asked by the original poster, and as such should really be in it's own thread. You might want to read "Use One Thread Per Question".

I thought you had your answer in your post "magic cookie and 0x800 flag doubt"

For more reading and ideas, take a look at:
  • NX: 0x8000?
  • NX: [Help] read & write flag on database


  • Regards, Andrew
    Jeffry Kristianto Yanuar
    Ranch Hand

    Joined: Oct 01, 2007
    Posts: 759
    Thanks Andrew, Those 2 thread is old thread. Can I access the other old thread?
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11527
        
    100

    I'm not entirely sure I understand your question, but here goes ...

    When looking at the SCJD forum landing page (or any of the forum pages) you should be able to see a pull-down list of how many posts you want to see. Many people keep it to the default of "Show topics from last 5 days", but you can set it to show more topics if you so desire - even set it to show all topics that have ever been posted to this forum.

    There is also a "Search" option on the line immediately below the "Post New Topic" button. Using the search feature you can find posts from anytime in the past in any forum.

    If I have answered a question you have not asked, I apologize.

    Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: B&S How to read numeric values from database file