Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile Help!  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this problem where I have to make a java program that manages an archive (file) with fixed-width records.

The records must be stored footage for at least 5 employees of a company fields.

The file management must allow the logical deletion of records.

The program must have methods to:
insert a new record in the space left by a deleted record or if there are not, at the end of file
delete a record by knowing the position
edit the fields of a record knowing the position
list all records in the file
shrink the file by removing deleted records

So far i only have this..

I don't know how to delete and modify them

Anybody help?

 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far, you haven't addressed the concept of "fixed length". Each record should be N bytes long. You must seek to a multiple of N. You must write no more than N bytes in a record. You could just come up with a value N that would handle your worst case. Ideally you'd check the record length before writing it to the file.

Next, you need to decide how to indicate that a record is deleted. You could, for instance, fill the record entirely with zeros, or just set the first byte (or four bytes) to 255 (assuming that normal data would not start with that value).

Before inserting a record you'd have to go through the file to find the first (if any) record that is "deleted". IF found, write the new record there, ELSE write new record to the end of the file.

This should be enough to get you started.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I'm digging up ancient history here, but one of the features of databases using fixed-length records is that the fields of the database also have fixed length. So the "name" field might be 20 bytes, or 25, or whatever the designer decides is suitable. Likewise the "surname" field would have a fixed length, probably the same length as "name". An int field like "age" would be stored in its binary form, so that would require 4 bytes. Adding them all up after you finish that bit of design tells you how many bytes each record requires.

Note that a fixed-length text field needs to be padded with blanks or truncated to the design length before you write it; if you wanted to support variable-length text fields then you could store the length as a separate (hidden) field but you'd still have to have a maximum length.

So far you don't seem to have done any of this design work, but you need to do it before you start programming.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Note that a fixed-length text field needs to be padded with blanks or truncated to the design length before you write it; if you wanted to support variable-length text fields then you could store the length as a separate (hidden) field but you'd still have to have a maximum length.

If the OP uses writeUTF() to write out the Strings then the length is included in the data written. Of course you'd then have to account for the additional bytes used to record the String length when coming up with a record length, and somehow the length would need to be verified before writing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!