Meaningless Drivel is fun!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S, hardcoding file headers 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, hardcoding file headers" Watch "B&S, hardcoding file headers" New topic
Author

B&S, hardcoding file headers

hatim osman
Ranch Hand

Joined: Jun 10, 2005
Posts: 105
Hi There...
my Data.java class looks something like this:

public class Data implements DB{
int RECORD_LENGTH;
long DATA_START_POINTER;
.
.
.
}

Now I was able to read the file and the length of each variable, should I hardcode the physical position of such variables specially the data_start_ptr, or have my class constructor or a helper method ex.
init(), take care of assigning values to those variables everytime the file is opened?
My idea is, in case someone change the format of the file, say, the length of a field then any data read or written will corrupt the file.
Any comment please.

Thanx in Advance
Hatim
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11432
    
  85

Hi Hatim,
My idea is, in case someone change the format of the file, say, the length of a field then any data read or written will corrupt the file.
This is a reasonable position to take. You could even take it a step further: since you know the size of all the fields and the header, it would be possible to calculate whether the size of the entire file (less the header) is a multiple of a record length - if it is not then the file is probably invalid.

However you might also like to think about a couple of things:
  • The supplied file comes with a "magic cookie" that presumably identifies the format of the file. As long as that magic cookie is correct, you can determine everything else (field lengths, therefore header length, therefore allowable file sizes).
  • There is nothing in the Data class methods that is specific to a contractors table. Change the underlying file, and you could access any type of data with the same class (as long as the file conforms to the format specified by the magic cookie). But if you hard code field lengths specific to the contractors table, then you not only stop yourself from being able to handle other types of data, you cannot even handle a change to the contractor's format without a recompile.

  • Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: B&S, hardcoding file headers