Actually you might find yourself writing a mini database system for
this to work
really well and fast enough for lookups and such, so a better approach
would
probably be to include hsql or a similar embeddable database in your
project.. Its
quite simple.
BUT if this is not an option I would suggest you make your record size
fixed and
drop the iRecordNumber in the structure. Then you have to write some
code which
get an exclusive lock on a fil, lets call it data.dat. Seek to last
position in file for inserts,
seek to position recordSize * iRecordNumber (index) for reads and
updates and use
a mechanism to mark a record as deleted if this is necessary. Then
create a sweeper
which goes thru the file once in a while, copying records to a new
file, but leaving the deleted once to clean things up.
regards, Lars Borup Jensen
GreenMountainBoy wrote:
> HELLO,
>
>
> Would anyone be able to advise me as to how to write a simple Random
> Access File I/O program.
>
>
> The record structure is
>
> public Employee
>
> {
> int iRecordNumber;
>
> String sName;
>
> double dSalary;
>
> }
>
>
> I need to be able to create these records from the keyboard, then write
> them to disk, and then be able to read one based on a iRecordNumber.
> Part of the problem is that the Name string is variable length and I
> need it to be a fixed 25 characters - I have found that character
> arrays won't function in many places where a string is called for..any
> simple solutions? I guess I could pad the string so that it is the
> right length, but the rest of the program still confuses me. any help
> would be forever remebered.
>
>
> I need to be able to 1) add new items, 2) list all items, 3) query an
> item based on its record number, 4) update a record (salary only), 5)
> delete an item.
>
>
> I have looked in all the standard Java texts for an example framework,
> but there is nothing comparable....
>
> THANKS SO MUCH FOR YOUR HELP... I REALLY APPRECIATE IT...
>
> p.s. I posted this on the java help forum but it seems that it requires
> the expertise that this forum provides.
>
>
> thanks, GreenMountainBoy
|