Well I'm into Andrews book a lot, and have a solution where I map recNo's to the location in the file, in a HashMap. The recNo is just an incrementing long in a loop which is executed at startup, where I map it to the location in the file. So far so good and everything works fine.
However, in the interface provided by Sun, it is stated in the createRecord-method, that it should return the recordnumber of the new number. Now I've managed to write to the file, but how do I know which is my next record number? In RDBMS you would probably to something like "SELECT MAX(id)" or something like that, but here my recordnumbers are stored in a HashMap and there is no way to find the "last inserted" in a HashMap. Or is there?
In Andrews book, he already has the record number ready since it is something he creates as a part of a DVD(UPC-number!?) whereas here it is just an incremental long which I saved during startup.
hi, in the create method where you decide where to store the record (I am guessing that you save the new record at the end without reusing deleted record) you get the writing position on the file before writing to the file using the RandomAccessFile's method length(). by doing simple calculations (length/(record length-header length)) you can get the record number.
hope that helps
SCBCD - SCWCD - SCJD - SCJP - OCA
Joined: Aug 18, 2005
thanks for the input. Never really thought about that, that I could calculate the new number with the help of the length etc. You wrote:
"(length/(record length-header length))"
Was that only a quick assumption or a definitive solution? I know record length is my total record length(size of all fields together) and the length of the file is obvious, but what is the header length? Do you mean the size from the start of the file to where the actualy records begin?