I have the contractor assignment and I started to read the db file. I have two questions 1.I don't understand how to read the record indicator flag. My instruction file contains: "Repeat to end of file: 1 byte flag. 00 implies valid record, 0xFF implies deleted record ". Because the 0xFF value extends the range of byte type, I think I have to use: RandomAccessFile raf= new RandomAccessFile("db"); short recordIndicator=raf.readShort();
The problem is, that for the first line I got 2048 and for the others 8192. This is not 00 and not 0xFF. I must cast the value to byte: byte recordInd=(byte)recordIndicator ?? 2. Where I have to use the magic cookie value? Thanks, Maria
0xFF is a valid byte so can use readByte to get it. Java interprets a byte (0x00 to 0xFF) as a 2's complement signed integer in the range -128 through +127. 0x00 through 0x7F is interpreted as 0 through 127. 0x80 through 0xFF is interpreted as -128 through -1. If you use readShort, you will read the next 2 bytes, so this is an incorrect approach. [ February 08, 2004: Message edited by: Ken Krebs ]
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
A guestion again. How can I reuse a deleted line? Read it and write the record indicator with the valid value(0)? But I read the record only if they are valid. I am confused now. I hope you know an answer. Maybe I should read it using the create method? But how I know if I want to reuse a deleted record or to create a new record? My create method has the following signature: int create(String data). Regards, Maria
Hi Maria, My recommendation is not to try to fix your current database file. I would make another copy of your original database file (you did make a copy of the original file you downloaded from Sun, didn't you) and start working from that. If by some chance you did destroy your original database file, I think you may be able to download the assignment again. It is very important to maintain a copy of your original database file, not only because your application may corrupt the file while you are testing, but also because you must submit an (unaltered) copy of your original database file with your project submission. Concentrate on reading the file in (the read method) before moving on to the implementation of the other database methods. I would focus only on reading the file in at first to get that part working before moving on to writing the file. Take it one step at a time, it's easier that way in my opinion, and you get to see some progress at an earlier time. Hope this helps, George [ February 08, 2004: Message edited by: George Marinkovich ]
Hi George, I have not understand your answer. I have already read all records and displayed in a JTable. My problem is that I have read all records witch are deleted, I have not validate the records again the record indicator flag. If I do this-if I read the records with a valid record indicator- then my table will be empty.I think, I must first create valid records an then do the read. Regards, Maria
I have already read all records and displayed in a JTable. My problem is that I have read all records witch are deleted, I have not validate the records again the record indicator flag. If I do this-if I read the records with a valid record indicator- then my table will be empty.I think, I must first create valid records an then do the read.
I recommend you start with a new copy of your original database file. To my knowledge this file contains only valid (that is, non-deleted records). So were you to read the database file, I think you would find that the validity flag for each of the records should have the value 0x00, indicating that the record is valid. It seems to me that your read method should first determine whether the record in question is valid or not. That is, it should read the validity flag and if it's 0x00 then read the record. If the validity flag is 0xFF then you should throw the RecordNotFoundException because the record in question has been marked for deletion. If still believe that all the record in your database file are marked for deletion, then I would suggest that you may not be reading the validity flag correctly. Check the database file layout section of the assignment instructions again and make sure that you are reading each element specified in the correct way: the proper number of bytes and the in the proper order. If you had a one-off error in your reading you could very well be seeing 0xFF as the validity flag of every database record. It's just not reasonable (possible yes, reasonable no) that all the records in the Sun-supplied database file would be marked for deletion. Hope this helps, George
Hi George, Thank you for your advise. I am now again at home after a full work day and I was very happy to see your answer. I will check now if I have read the validity flag correctly. It is seems to be correctly because in the table each column is properly filled- but maybe there are some mistakes. Regards, Maria.