NX: (Contractors) File I/O class

Hi guys, I have Contractors assignment. The instruction mentioned DataInputStream and DataOutputStream classes. Does that mean I have to use these two classes to read and write the db file? Nitti

SCJP, SCWCD, SCJD, (SCEA, MCSD.net)

Jim Yingst

Wanderer
Sheriff

Posts: 18671

posted 13 years ago

Not necessarily. RandomAccessFile implements the same methods. You could also write your own methods to (for example) convert four consecutive bytes into an int - it's pretty simple. But there's no good reason to do this, that I see; you might as well just use one of the classes already available. Note - I use a FileChannel to read the main part of the file, because it's much faster. FileChannel doesn't have the DataInput / DataOutput methods. If I were to read the header with FileChannel, I'd have to do a little more work to extract the data. But instead I just use RandomAccessFile for the header (slower, but short) and save the FileChannel for the main body.

"I'm not back." - Bill Harding, Twister

Thomas Kijftenbelt

Ranch Hand

Posts: 73

posted 13 years ago

Hi, I agree with Jim, I also use a FileChannel on top of the RandomAccessFile. To be honest: I also use it for obtaining the header information... - get a FileChannel from your randomAccessFile - create a byteBuffer which contains the data from your FileChannel - use the methods provided in the ByteBuffer to extract the various datatypes Greetings, TK

Nitti Lin

Ranch Hand

Posts: 39

posted 13 years ago

Thank you guys... I will check it out. Nitti

SCJP, SCWCD, SCJD, (SCEA, MCSD.net)

Billy Tsai

Ranch Hand

Posts: 1304

posted 13 years ago

Hi I have the same assignment but I dont really understand the data file format described since I am used to accessing a relational database I have never used a flat file database. what does Start of file 4 byte numeric, magic cookie value. Identifies this as a data file 2 byte numeric, number of fields in each record mean , also the Data section, and schemea description section how do I go about in reading and writing the data in that file and each fields name? btw my file is db-2x3.db any help and explanation would be much appreciated . Thanks

I think the magic cookie is for Sun's people or program to identify the database format for their internal use. In my case, the magic cookie is 0023 if I didn't read it wrong.

Originally posted by Billy Tsai:

2 byte numeric, number of fields in each record

It's just what it says. If you don't want to hardcode the number of fields, you may use this value to read how many fields in this database file format. At least, that's what I'm doing. I have a for loop using this value to loop through the schemea description and save it.

Originally posted by Billy Tsai:

also the Data section, and schemea description section how do I go about in reading and writing the data in that file and each fields name?

In my case, I have a method to read the file header and save the field names and field length. I also save the position of the first record after I'm done reading the header.

Originally posted by Billy Tsai:

btw my file is db-2x3.db

I have the same database file. Just double check... do you have 34 records as well? Nitti

SCJP, SCWCD, SCJD, (SCEA, MCSD.net)

Billy Tsai

Ranch Hand

Posts: 1304

posted 13 years ago

Thanks for your explanation are you using NIO , fileChannel and RandomAccessFile to aceess the file?

HI I need to clarify something, in the (NX)contractor assignment even though we have the delete and create methods defined in the interface but we don't really have to implement them as those function are not used on the GUI is that right so whether to write the codes and implement those function is up to us am I correct ? but not necessary have to any explanationo would be appreciated thanks

Hi Billy, Initially I also did not plan to implement create / delete (just throw operationnotsupported exception); however if you have your update method, the create and delete methods can be implemented within half an hour... I implemented them now, and it is very helpful for testing purposes. I'll probably leave them in... Greetings, TK

Billy Tsai

Ranch Hand

Posts: 1304

posted 13 years ago

Hi thanks for your insight but is it a MUST to have fully working create and delete functionalities according to Sun's requirement? is it okay to not have the create and delete methods written if I dont do them am I still able to pass SCJD? thanks

Hi Billy, To be honest: I am not sure anymore whether it is OK to leave them blank... Maybe Max has an answer to this question... TK

Jim Yingst

Wanderer
Sheriff

Posts: 18671

posted 13 years ago

We're not really sure about this, but see this previous discussion (starting at the eleventh post or so, where I say "We're not?"). You may be able to pass without providing a working implementation of create() and delete() - but I'd expect you'd at least lose points for it. Why risk it?

"I'm not back." - Bill Harding, Twister

Max Habibi

town drunk( and author)
Sheriff

Posts: 4118

posted 13 years ago

I think you need to have them, but I don't think you need to provide access to them through the gui. M

I'm pretty much done with the Contractor project. My GUI client provides Search, update, delete, create, and set owner function. I personly do think that Create/Delete methods are required because those are the most basic function. Nitti

SCJP, SCWCD, SCJD, (SCEA, MCSD.net)

S. Ganapathy

Ranch Hand

Posts: 194

posted 13 years ago

Hi Nitti, Contractor assignment requirement does not have delete functionality. Only search, and update(booking by providing owner field) only. How did you provide search? search is only on name and location or on all fields? Regards, Ganapathy

Steven Wexelstein

Greenhorn

Posts: 1

posted 13 years ago

Is adding the delete and create functionality in the GUI not out of the scope of the requirement. I think implementing it on the server side may well be okay but giving the user access without the requirment doesn't feel right.

S. Ganapathy

Ranch Hand

Posts: 194

posted 13 years ago

ya, in GUI, there is no create/delete functionality.

Jim Yingst

Wanderer
Sheriff

Posts: 18671

posted 13 years ago

For that matter, the GUI is not required to (and probably should not) provide a general update function that allows users to change any field other than owner (customer ID). The only change of any type which the GUI is required to perform is to change the customer ID, to book a record (if it's not already booked by another customer). It may also be reasonable to provide cancel booking functionality (set a customer ID to blank), but this is not necessarily required. Judgement call; I'd include it.

"I'm not back." - Bill Harding, Twister

Nitti Lin

Ranch Hand

Posts: 39

posted 13 years ago

I should read the requirement again, but I think the "users" are the customer representives, not customers themself. Customers call the representives, and the customer representives use client program to find a contractor for them. This is my understanding of who is going to use the client program. Since users are customer representives, I think they should be able to update or modify the data. I will check out the requirement again. Nitti

SCJP, SCWCD, SCJD, (SCEA, MCSD.net)

Nitti Lin

Ranch Hand

Posts: 39

posted 13 years ago

Originally posted by S. Ganapathy: How did you provide search? search is only on name and location or on all fields?

Since users are customer representives, I think they should be able to update or modify the data. Maybe - we can argue this either way. Many companies might mant to limit what people can do - maybe the CSRs have not been fully trained in how the data are to be formatted, e.g. what if they leave off the $ in the rate field, or use "and" where "&" is expected, or use capitalization incorrectly, or just can't spell very well. Power to edit/create/delete arbitrary fields may be intentionally reserved to trusted individuals who will use a different program. Or not. We're required to allow for editing of the customer ID field; any other updates are optional as fare as requirements are concerned.

"I'm not back." - Bill Harding, Twister

Max Habibi

town drunk( and author)
Sheriff

Posts: 4118

posted 13 years ago

I would say that they're not even optional. The client has told you what they want. Thus, in your bid to prove to Sun that you're a professional grade developer, I would say that you should code to the specs rigorously. Thus, no failures, but no 'features'. It could be argued that you don't really have the right make the client wait longer for the product, just because you want to give them things they didn't ask for . M